【Selenium异常】NoSuchElementException

来源:互联网 发布:网上投票系统 源码 编辑:程序博客网 时间:2024/06/18 00:24

异常类:

org.openqa.selenium.NoSuchElementException

可能报错原因如下:

1.元素定位路径错误

堆栈信息:[1]
解决方案:修改定位路径,并先在浏览器中进行测试

2.页面(DOM)未加载完成

说明:如果你的元素定位路径中有些节点(元素节点、文本节点等)是通过JS生成的,当DOM加载完成时,JS可能还在执行,而selenium判断页面加载完成应该是不考虑JS的,所以可能会出现页面实际未全部加载完成的情况。

堆栈信息:[1]
解决方案:
(1)线程等待:如1000毫秒;

try {    Thread.sleep(1000);} catch (InterruptedException e) {    e.printStackTrace();}

(2)显示等待:超时时间内,轮询查找,selenium2中默认轮询时间为500毫秒一次。

//示例代码:超时时间内轮询查找//或者自己设置轮询时间://new WebDriverWait(driver, timeOutInSeconds, sleepInMillis);WebElement we = new WebDriverWait(driver, 3).until(new ExpectedCondition<WebElement>() {    @Override    public WebElement apply(WebDriver driver) {        return driver.findElement(By.id("testid"));    }});//selenium2源码//参见类:org.openqa.selenium.support.ui.FluentWait<T>//参见方法:public <V> V until(Function<? super T, V> isTrue)//以下为轮询时间设置,重点的三句代码public static final Duration FIVE_HUNDRED_MILLIS = new Duration(500, MILLISECONDS);private Duration interval = FIVE_HUNDRED_MILLIS;sleeper.sleep(interval);

(3)隐式等待:全局设置,针对所有元素,超时时间内轮询查找。

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
3.driver焦点不在元素所在frame

堆栈信息:[1]
解决方案:
(1)将WebDriver的焦点切换到元素所在Frame或IFrame。


[1]堆栈信息:Unable to locate element

org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//td[text()='i存-60天a']/../td[4]"}  (Session info: chrome=60.0.3112.113)  (Driver info: chromedriver=2.31.488763 (092de99f48a300323ecf8c2a4e2e7cab51de5ba8),platform=Windows NT 6.1.7600 x86_64) (WARNING: The server did not provide any stacktrace information)Command duration or timeout: 248 millisecondsFor documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.htmlBuild info: version: '2.53.0', revision: '35ae25b1534ae328c771e0856c93e187490ca824', time: '2016-03-15 10:43:46'System info: host: '***', ip: '10.200.4.94', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_77'Driver info: org.openqa.selenium.remote.RemoteWebDriverCapabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.31.488763 (092de99f48a300323ecf8c2a4e2e7cab51de5ba8), userDataDir=C:\Users\Think\AppData\Local\Temp\scoped_dir15164_7953}, takesHeapSnapshot=true, pageLoadStrategy=normal, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=60.0.3112.113, platform=XP, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true, setWindowRect=true, unexpectedAlertBehaviour=}]Session ID: a1a2fea39231c8ce15740b305298d9c5*** Element info: {Using=xpath, value=//td[text()='i存-60天a']/../td[4]}    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[?:1.8.0_77]    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) ~[?:1.8.0_77]
阅读全文
0 0