硒找不到元素 [英] Selenium can't find element

查看:40
本文介绍了硒找不到元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

我使用的命令是:

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

我正在使用:

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?

推荐答案

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

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

按照 OP 的评论运行 xpath

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

这篇关于硒找不到元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆