user2869231
user2869231

Reputation: 1541

Selenium can't find element

I am having trouble accessing elements:

<fieldset>
  <legend>
    Legend1
  </legend>
  <table width=100%" cellspacing="3" bgcolor="white">
    <tbody>
      <tr>...</tr>
      <tr>...</tr>
    </tbody>
  </table>
  <fieldset>
    <legend>
      Legend2
    </legend>
    <table width="100%" cellspacing="3" bgcolor="white" align="center">
      <tbody>
        <tr>
          <td></td>
          <td class="reportLabel" nowrap="">Label1</td>
          <td class="reportField>Field1</td>
          <td></td>
        </tr>
      </tbody>
    </table>
    <fieldset>
       ...

I can access everything in the first table (before entering a sub-fieldset). However, I can't access anything from the fieldset on. The error I get is:

Message: Unable to find element with xpath == ...

Is there something special I have to do when there are new fieldsets? Similar to having to switch frames?

The command I'm using is:

ret = self.driver.find_element_by_xpath("//fieldset/legend[text()='Legend2']/following::table/tbody/tr/td[@class='reportlabel'][text()='Label1']")

The reason I'm including the legend and following it with 'following' is that there are a lot of different sections and legends within a previous one, and I'd like to ensure that the field is indeed in the proper section.

I have also tried simpler things, though, such as:

ret = self.driver.find_element_by_xpath("//fieldset/table/tbody/tr/td[@class='reportLabel][text()='Label1']")

I am using:

IE11 (same issue on Firefox, though)
Selenium 2.44.0
Python 2.7
Windows 7
32 bit IEDriverServer.exe

Does anyone know why I can't access these elements?

Upvotes: 3

Views: 1488

Answers (1)

Saifur
Saifur

Reputation: 16201

Your second XPATH looks correct unless the fact that you are missing a ' after reportLabel. Corrected:

//fieldset/table/tbody/tr/td[@class='reportLabel'][text()='Label1']

Working xpath as per OP's comment

//legend[contains(.,'Legend2')]//..//td[contains(text(),'Label1')]

Upvotes: 3

Related Questions