【发布时间】:2017-06-25 23:38:35
【问题描述】:
我正在尝试使用Xpath 点击一个链接(看起来像一个标签按钮),但出现错误。
这是html sn-p:
<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();
这是错误:
线程“主”org.openqa.selenium.NoSuchElementException 中的异常: 没有这样的元素:无法找到元素: {"方法":"xpath","选择器":"/html/body/div[5]/div[2]/div"}
(会话信息:chrome=54.0.2840.87)(驱动程序信息: 铬驱动程序=2.27.440174 (e97a722caafc2d3a8b807ee115bfb307f7d2cfd9),平台=Windows NT 10.0.10586 x86_64)(警告:服务器未提供任何堆栈跟踪信息)命令持续时间或超时:412 毫秒 有关此错误的文档,请访问: http://seleniumhq.org/exceptions/no_such_element.html 构建信息: 版本:'未知',修订:'1969d75',时间:'2016-10-18 09:43:45 -0700' 系统信息:主机:'tmlpnedtp061674',ip:'172.22.84.78',os.name:'Windows 10',os.arch:'amd64',os.version:'10.0', java.version: '1.8.0_101' 驱动信息: org.openqa.selenium.chrome.ChromeDriver 功能 [{applicationCacheEnabled=false, 可旋转=false, mobileEmulationEnabled=false,networkConnectionEnabled=false, 铬={铬驱动程序版本=2.27.440174 (e97a722caafc2d3a8b807ee115bfb307f7d2cfd9), userDataDir=C:\Users\AMITPA~1.TTL\AppData\Local\Temp\scoped_dir11224_7493}, 需要HeapSnapshot=true,pageLoadStrategy=正常, databaseEnabled=false,handlesAlerts=true,hasTouchScreen=false, 版本=54.0.2840.87,平台=XP,browserConnectionEnabled=false, nativeEvents=true,acceptSslCerts=true,locationContextEnabled=true, webStorageEnabled=true,browserName=chrome,takeScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true, unexpectedAlertBehaviour=}] 会话 ID: 2bb2aa6d378f2c0f6ec720a9ee87068c *** 元素信息:{Using=xpath, value=/html/body/div[5]/div[2]/div} at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native 方法)在 sun.reflect.NativeConstructorAccessorImpl.newInstance(未知来源) 在 sun.reflect.DelegatingConstructorAccessorImpl.newInstance(未知 来源)在 java.lang.reflect.Constructor.newInstance(未知来源) 在 org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:216) 在 org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:168) 在 org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:635) 在 org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:368) 在 org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:473) 在 org.openqa.selenium.By$ByXPath.findElement(By.java:361) 在 org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:360) 在 testProj001.CordysUpgradeDemo.main(CordysUpgradeDemo.java:19)
在屏幕截图中,您可以看到我正在尝试单击“建议方案”按钮链接
【问题讨论】:
-
请帮助我们,因为我是硒自动化的新手。
-
分享
HTML提到按钮的代码 -
您使用的是定位
xpath,它不会更稳定,为了更好的解决方案,您需要共享相关的 HTML.. -
如果有帮助,我可以在这里分享 HTML 吗??
-
@AmitPandey 您可以在编辑问题后分享 HTML ..
标签: java selenium xpath selenium-webdriver