0

My overall goal is to iterate through every tr in this table and go through several td's per tr and print out the text in them.

I've tried going back as far as the body yet still no luck. ideally I would like to just run a forloop through table[@id='aui']/tbody/tr[i]/td[i] where i is the variable in the range(0, sizeOfTable)

ele = driver.find_elements_by_xpath("//body/div[@id='page']/section[@id='content']/div[@class='aui-page-panel']/div[@class='aui-page-panel-inner']/section[@class='aui-page-panel-content']/div[@class='aui-page-panel']/div[@class='aui-page-panel-inner']/section[@class='aui-page-panel-content']/div[@class='module']/div[@id='projects']/div[@class='p-list']/table[@id='aui']/tbody/tr[1]/td[1]")

right now when I run print(ele.text) after creating ele, it just prints out an empty list

this is the html I'm working off of:

1
  • Please paste code as text here, you can format it with the {} button, don't paste pure text content as picture here. Commented Jul 18, 2019 at 1:28

1 Answer 1

2

Reason for the issue is a wrong XPath. The table has class with aui but not id.

Correct the XPath as below.

  //table[@class='aui']//tr

This should give you a list of rows and then you can iterate through them.

Script: if you want to print the text in the cell then use this.

rows = driver.find_element_by_css_selector("table.auti  tr")
for row in rows:
    # get the hrefs
    columsWithLink = row.find_element_by_xpath(".//td[a]")
    for column in columsWithLink:
        # print column text
        print (column.text)
        # print link href
        print(column.find_element_by_xpath(".//a").get_attribute("href"))

Script: if you just want to print link hrefs then use the below

rows = driver.find_element_by_css_selector("table.auti  tr")
for row in rows:
    # get the hrefs
    links = row.find_element_by_xpath(".//td/a")
    for link in links:
        # print link href
        print(link.get_attribute("href"))
Sign up to request clarification or add additional context in comments.

8 Comments

so im trying to print out the highlighted text in the html: ".com Ownership support", but in your code you jump from table to tr, dont i have to include the tbody?
Not required when you are dealing with css.
If you are trying to get the text associated with link, then try column.find_element_by_xpath(".//a").text and link.text respectively in the above answer.
i got the text thank you! would you happen to know how to work off using a html doc instead of a url link as a webdriver? i have to log in each time to test the site so i want to skip it all
Cool. If you want to load the local html doc then you can use url = r"file:///C:/Users/xxx/Desktop/sample.html" and then driver.get(url) should load the local page.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.