硒找不到元素

Selenium can't find element

我在访问元素时遇到问题:

<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>
       ...

我可以访问第一个 table 中的所有内容(在进入子字段集之前)。但是,我无法从 fieldset 上访问任何内容。我得到的错误是:

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

当有新的字段集时,我必须做些什么特别的事情吗?类似于必须切换帧?

我使用的命令是:

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

我加入图例并在其后加上 'following' 的原因是前一个中有很多不同的部分和图例,我想确保该字段确实是在适当的部分。

不过,我也尝试过更简单的方法,例如:

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

我正在使用:

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

有谁知道为什么我无法访问这些元素?

你的第二个 XPATH 看起来是正确的,除非你在 reportLabel 之后缺少 '。更正:

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

根据 OP 的评论xpath工作

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