python - Selenium 找不到元素

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

我可以访问第一个表中的所有内容(在进入子字段集之前)。但是,我无法从 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']")

我包含图例并在其后加上“跟随”的原因是在前一个中有很多不同的部分和图例,我想确保该字段确实在正确的部分中.

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

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

关于python - Selenium 找不到元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28813670/

相关文章:

python - 运行测试时 Django 1.5.1 'ImportError: No module named urls'

javascript - 什么可能是 javascript 中 iteritems() [来自 python] 的替代品

xml - XSLT 1.0 : copy everything except certain nodes according to value and variable

使用边界的 Python/Django mime 多部分电子邮件

python - wxPython 创建图像工具栏唯一窗口

selenium - "selenium server"和 "selenium server standalone"jar 之间的区别

java - 如何永久解决 Jenkins 中显示范围报告的 HTML Publisher 插件问题?

javascript - vb.net - 执行 selenium ChromeDriver javascript

java - 如何在 selenium 中抛出自定义异常而不是 NoSuchElementException

xml - 复杂的 xPath 查询