java如何构造一个xpath,以单击以文本作为操作的元素
<div class="toolbar">
<table>
<tbody>
<tr>
<td class="text-button menu-button-active" itemid="sdfsgsg.0978" title="Actions" norap="">
Actions
<img src="../common/ads.gif">
</td>
</tr>
</tbody>
</table>
img
是td的子标记,类属性值是动态的
错误日志:
org.openqa.selenium.NoSuchElementException: Unable to locate element: //div[@class='toolbar']//td[text()='Actions']
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.13.0', revision: '2f0d292', time: '2018-06-25T15:32:14.902Z'
os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_131'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities {acceptInsecureCerts: true, browserName: firefox, browserVersion: 52.9.0, javascriptEnabled: true, moz:accessibilityChecks: false, moz:processID: 9268, pageLoadStrategy: normal, platform: XP, platformName: XP, platformVersion: 10.0, rotatable: false, specificationLevel: 0, timeouts: {implicit: 0, page load: 300000, script: 30000}}
Session ID: 0f4515d3-fad9-4c70-b50a-27f6c9abd249
*** Element info: {Using=xpath, value=//div[@class='toolbar']//td[text()='Actions']}
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:548)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:322)
at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:424)
at org.openqa.selenium.By$ByXPath.findElement(By.java:353)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:314)
at PCS.PCS_TEst.BaseClass.CreateColor(BaseClass.java:100)
# 1 楼答案
因为文本中有空格,所以需要使用
normalize-space
清除它们:# 2 楼答案
如果img是td的子元素,请尝试以下方法
如果img是td的sibling元素,那么试试这个
希望这有帮助:)
# 3 楼答案
Xpath是:
# 4 楼答案
如果你只想要“td”元素,那么使用css获得它,比如:
如果你想点击内部图像元素,可以使用:
希望这对你有帮助
使用xpath: 对于行动“td”元素:
对于内部img元素:
# 5 楼答案
根据HTML,您可以使用以下xpath:
更新:
此错误消息
。。。这意味着壁虎河无法通过定位策略
//div[@class='toolbar']//td[text()='Actions']
定位元素根据你分享的HTML,你需要通过WebDriverWait诱导一个服务员,你可以使用以下解决方案:
但是您的主要问题是您正在使用的二进制文件版本之间的不兼容,如下所示:
解决方案:
@Test
李>