简体   繁体   English

硒找不到元素

[英]Selenium can't find element

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? 有谁知道为什么我无法访问这些元素?

Your second XPATH looks correct unless the fact that you are missing a ' after reportLabel . 你的第二个XPATH看起来是正确的,除非你错过了' after reportLabel Corrected: 更正:

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

Working xpath as per OP's comment 根据OP的评论工作xpath

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM