Iterate through table rows and print column text with Python Selenium

后端 未结 2 1080
我在风中等你
我在风中等你 2020-12-24 07:32

I have a table (

) with values in each row () from its body ().

The value I would lile to p

相关标签:
2条回答
  • 2020-12-24 08:09

    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)
    
    0 讨论(0)
  • 2020-12-24 08:12

    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 table
    for 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
    
    0 讨论(0)
提交回复
热议问题