Python Forum
How to get specific TD text via Selenium?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to get specific TD text via Selenium?
#1
I try to scrape a table and read specific TD in Table in each row. But for some reasons it fails at the last step. What I'm doing wrong?

content = driver.find_element_by_id("searchResultTable").find_elements_by_tag_name("tr")
for contents in content:
    pop(contents.find_elements_by_tag_name("td")[9].text, "Value of 9th")
this example works, but it's a bit lame to loop through every TD until the index match. There should be a way to contact a TD directly with [Index]

content = driver.find_element_by_id("searchResultTable").find_elements_by_tag_name("tr")
for contents in content:
    td = contents.find_elements_by_tag_name("td")
    for ind, ttd in enumerate(td):
        if ind == 9:
           pop(ttd.text, "Value of 9th")
Reply
#2
what is the URL?
Reply
#3
that's a company webpage, so I cannot provide an url, but I think the problem is somewhere in the code itself.
Reply
#4
Can use CSS selector .find_element_by_css_selector
If getting first name eg Jill, body > table > tbody > tr:nth-child(2) > td:nth-child(1)(can copy selector from Browser).
Then the loop to get would be like this for getting all first name in table.
for n in range(2, 5):
    tag = f'body > table > tbody > tr:nth-child({n}) > td:nth-child(1)'
    print(tag)
Output:
body > table > tbody > tr:nth-child(2) > td:nth-child(1) body > table > tbody > tr:nth-child(3) > td:nth-child(1) body > table > tbody > tr:nth-child(4) > td:nth-child(1)
<table style="width:100%">
  <tr>
    <th>Firstname</th>
    <th>Lastname</th>
    <th>Age</th>
  </tr>
  <tr>
    <td>Jill</td>
    <td>Smith</td>
    <td>50</td>
  </tr>
  <tr>
    <td>Eve</td>
    <td>Jackson</td>
    <td>94</td>
  </tr>
</table>
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  selenium wait for text question Larz60+ 2 56 Yesterday, 03:02 PM
Last Post: Larz60+
  Selenium innerHTML list, print specific value denis22934 2 662 Jun-14-2021, 04:59 AM
Last Post: denis22934
  With Selenium create a google Search list in Incognito mode withe specific location, tsurubaso 3 1,042 Jun-15-2020, 12:34 PM
Last Post: tsurubaso
  Selenium extract id text xzozx 1 882 Jun-15-2020, 06:32 AM
Last Post: Larz60+
  Getting text using Selenium WiPi 10 1,932 Apr-27-2020, 08:58 AM
Last Post: WiPi
  Web crawler extracting specific text from HTML lewdow 1 1,723 Jan-03-2020, 11:21 PM
Last Post: snippsat
  Selenium returning web element instead of desired text newbie_programmer 1 2,506 Dec-11-2019, 06:37 AM
Last Post: Malt
  Getting a specific text inside an html with soup mathieugrimbert 9 11,375 Jul-10-2019, 12:40 PM
Last Post: mathieugrimbert
  XML Parsing - Find a specific text (ElementTree) TeraX 3 2,439 Oct-09-2018, 09:06 AM
Last Post: TeraX
  webscraping - failing to extract specific text from data.gov rontar 2 1,749 May-19-2018, 08:01 AM
Last Post: rontar

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020