Selenium/Python: Find <label for=""> element with no other attributes

14,287

Solution 1

You can select the appropriate td tag using driver.find_element_by_xpath(). The XPath expression that you should use is as follows:

`'//label[@for="Rooms"]/parent::td/following-sibling::td'`

This selects the label tag with for attribute equal to Rooms, then navigates to its parent td element, then navigates to the following td element.

So your code will be:

elem = driver.find_element_by_xpath(
     '//label[@for="Rooms"]/parent::td/following-sibling::td')

An example of the XPath expression in action is here.

Solution 2

With xpath, you can create a search for an element that contains another element, like so:

elem = driver.find_element_by_xpath('//tr[./td/label[@for="Rooms"]]/td[2]')

The elem variable will now hold the second td element within the "Rooms" label row (which is what you were looking for). You could also assign the tr element to the variable, and then work with all of the data in the row since you know the cell structure (if you would like to work with the label and data).

Share:
14,287
Thanados
Author by

Thanados

Updated on June 04, 2022

Comments

  • Thanados
    Thanados almost 2 years

    I want to recover a number that is located in the following table: the site

    <table class="table table-hover table-inx">
     <tbody><tr>
      </tr>
      <tr>
      </tr>
      <tr>
      </tr>
      <tr>
      <td class=""><label for="RentNet">Miete (netto)</label></td>
      <td>478,28 €</td>
      </tr>
      <tr>
      </tr>
      <tr>
      </tr>
      <tr>
      <td class=""><label for="Rooms">Zimmer</label></td>
      <td>4</td>
      </tr>
      </tbody></table>
    

    I suppose this strange format happens because the table entries are optional. I get to the table with driver.find_element_by_css_selector("table.table.table-hover") and I see how one could easily iterate through the <tr> tags. But how do I find the second <td> holding the data, in the <tr> with the <label for="Rooms"> ? Is there a more elegant way than "find the only td field with a one-digit number" or load the detail page?

    This similar question didn't help me, because there the tag in question has an id

    EDIT:

    I just found out about a very helpful cheat sheet for Xpath/CSS selectors posted in an answer to a related question: it contains ways to reference child/parent, next table entry etc

  • Andrew Regan
    Andrew Regan about 8 years
    I didn't downvote you, but although XPath has its places, a selector like "/html/body/div[2]/div[7]/div[2]/div[3]/div/div[1]/div/div[3‌​]/div[3]/div/table/t‌​body/tr[7]/td[2]" has no semantic information and is unbelievably brittle - which makes for a very bad test. It's neat that Chrome and FF can generate selectors for us, but sometimes they're absolutely terrible.
  • Brandon
    Brandon about 8 years
    I understand 100%. I was just using the XPath Firebug gave me, which could be a useful tool for beginners not well-versed with XPath semantics who want to write their own programs. No doubt is it better to learn XPath than constantly relying on Firebug.
  • Thanados
    Thanados about 8 years
    works well and the xpath fiddling site is quite helpful
  • Thanados
    Thanados about 8 years
    I didn't downvote either. My problem with your example is that is assumes the tag will always be there. There could be out of bound errors, I believe?