How to get specific TD text via Selenium? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html) +--- Thread: How to get specific TD text via Selenium? (/thread-33655.html) |
How to get specific TD text via Selenium? - euras - May-14-2021 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") RE: How to get specific TD text via Selenium? - Larz60+ - May-14-2021 what is the URL? RE: How to get specific TD text via Selenium? - euras - May-14-2021 that's a company webpage, so I cannot provide an url, but I think the problem is somewhere in the code itself. RE: How to get specific TD text via Selenium? - snippsat - May-14-2021 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)
<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> |