硒找不到元素

时间:2015-03-02 15:39:41

标签: python selenium xpath

我无法访问元素:

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

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

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']")

我之所以包含传奇,并跟随&#39;跟随&#39;是因为在前一个部分中有很多不同的部分和图例,我想确保该字段确实在正确的部分。

我也尝试过更简单的事情,例如:

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

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

1 个答案:

答案 0 :(得分:3)

您的第二个XPATH看起来是正确的,除非您在'之后遗漏了reportLabel。校正:

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

根据OP的评论工作xpath

//legend[contains(.,'Legend2')]//..//td[contains(text(),'Label1')]
相关问题