我在访问元素时遇到问题:
<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']")
我包含图例并在其后面加上“以下”的原因是,前一个部分中有很多不同的部分和图例,我想确保该字段确实位于正确的部分中。
不过,我也尝试过更简单的事情,例如:
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
看起来是正确的,除非您缺少一个'
after reportLabel
。更正:
//fieldset/table/tbody/tr/td[@class='reportLabel'][text()='Label1']
Working xpath
根据OP的评论
//legend[contains(.,'Legend2')]//..//td[contains(text(),'Label1')]
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)