java - 在 Selenium 3 中使用 xpath 定位元素时出现错误

标签 java selenium xpath selenium-webdriver

我尝试使用 Xpath 单击链接(看起来像选项卡按钮),但出现错误。

这是 html 片段:

<div id="tile_8" style="height: 93px; width: 26%; background-color: rgb(45, 87, 19); color: white; position: relative; float: left; margin: 10px; padding: 5px; cursor: pointer; z-index: 1; border: 2px solid white;" class="divAppList-shadow" onmouseover="mouseOvering(event,'8')" onmouseout="mouseOuting(event,'8')">
	<b>
	<span style="font-size:medium; z-index:1; color:white;">Suggestion Scheme</span>
	</b><br>
	<span style="text-align:justify; z-index:1;color:white;">There is always a better way of doing a thing. You only can suggest it for us to Improve.</span>
 </div> 

我尝试过以下代码:

driver.findElement(By.xpath("/html/body/div[5]/div[2]/div")).click();

这是错误:

Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div[5]/div[2]/div"}
(Session info: chrome=54.0.2840.87) (Driver info: chromedriver=2.27.440174 (e97a722caafc2d3a8b807ee115bfb307f7d2cfd9),platform=Windows NT 10.0.10586 x86_64) (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 412 milliseconds For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html Build info: version: 'unknown', revision: '1969d75', time: '2016-10-18 09:43:45 -0700' System info: host: 'tmlpnedtp061674', ip: '172.22.84.78', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_101' Driver info: org.openqa.selenium.chrome.ChromeDriver Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.27.440174 (e97a722caafc2d3a8b807ee115bfb307f7d2cfd9), userDataDir=C:\Users\AMITPA~1.TTL\AppData\Local\Temp\scoped_dir11224_7493}, takesHeapSnapshot=true, pageLoadStrategy=normal, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=54.0.2840.87, platform=XP, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true, unexpectedAlertBehaviour=}] Session ID: 2bb2aa6d378f2c0f6ec720a9ee87068c *** Element info: {Using=xpath, value=/html/body/div[5]/div[2]/div} at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) at java.lang.reflect.Constructor.newInstance(Unknown Source) at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:216) at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:168) at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:635) at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:368) at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:473) at org.openqa.selenium.By$ByXPath.findElement(By.java:361) at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:360) at testProj001.CordysUpgradeDemo.main(CordysUpgradeDemo.java:19)

网站截图:: enter image description here

在屏幕截图中,如您所见,我正在尝试单击建议方案按钮链接

最佳答案

您可能没有等待页面完全加载。 尝试等待页面完全加载后再尝试单击它。如果 xpath 是正确的,那么它一定是这样的。

要进行测试,请使用 Chrome 访问网页。按 F12 打开开发人员工具。转到控制台选项卡并输入以下内容:$x("/html/body/div[5]/div[2]/div") 检查结果。如果它返回正确的元素,那么您就有正确的 xpath。如果没有,请尝试以下操作:

  1. //div[@id='tile-8']

  2. //div[@id='tile-8']/span[text()='建议方案']

  3. //span[text()='建议方案']

如果选项 1 有效,只需使用 By.id("tile-8") 而不是 By.xpath()

关于java - 在 Selenium 3 中使用 xpath 定位元素时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42111668/

相关文章:

xml - 为什么//后代也在这个 XSLT 模板中评估 sibling ?

python - 如何使用 Selenium 和 Python 定位具有多个类名的元素

java - 如何不断更新按钮颜色 [JAVA SWING]

java - Netbeans 中的文件结构

java - Linux 上 Java 和 Zeroconf (avahi) 的多播问题

selenium - 如何在移动模拟模式下运行 Selenium Python?

css - 如何在 map 中随机定位光标

java - openbsd:限制网络带宽

java - 在selenium中如何继续运行脚本,即使它失败了

java - 与 Selenium Grid 并行运行 TestNG 套件