3

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?

1 Answer 1

3

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')]
Sign up to request clarification or add additional context in comments.

8 Comments

How about //legend[contains(.,'Legend2')]//..//td[contains(text(),'Label1')]?
that worked! With that being said, though, what is the contains(., 'Legend2') doing? I know in Unix, a single dot means 'current location'
ah. Why would that be necessary, then? I figured selenium knew
Not sure what you mean but you should be able to do //td[contains(text(),'Label1')] as well
as much as I love CSS.. i can't stand how you can do something like .. in it :(
|

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.