Iterate through table rows and print column text with Python Selenium Iterate through table rows and print column text with Python Selenium python python

Iterate through table rows and print column text with Python Selenium


The developer has put an ID into the table. I have it working now. It is printing all the cell values from column 2. The code is:

table_id = self.driver.find_element(By.ID, 'data_configuration_feeds_ct_fields_body0')rows = table_id.find_elements(By.TAG_NAME, "tr") # get all of the rows in the tablefor row in rows:    # Get the columns (all the column 2)            col = row.find_elements(By.TAG_NAME, "td")[1] #note: index start from 0, 1 is col 2    print col.text #prints text from the element


The XPath you currently using is quite fragile since it depends on the complete document structure and the relative position of the elements. It can easily break in the future.

Instead, locate the rows using their class or other attributes. For instance:

for row in driver.find_elements_by_css_selector("tr.GAT4PNUFG.GAT4PNUMG"):    cell = row.find_elements_by_tag_name("td")[1]    print(cell.text)