首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >selenium-java有时会使用firefoxdriver运行异常

selenium-java有时会使用firefoxdriver运行异常
EN

Stack Overflow用户
提问于 2016-01-25 16:33:36
回答 2查看 344关注 0票数 0

这是我的java代码。

代码语言:javascript
复制
WebDriver driver = new FirefoxDriver();
for (DBTable table : tables) {
	try {
		Thread.sleep(10000l);
		String content = templeteService.generateTemplate(table);
		content = content.replaceAll("\\r|\\n|\\t", "");
		String title = table.getComment();
		//跳转到内容父页面
		driver.get(topUrl);
		String curUrl = driver.getCurrentUrl();
		//如果重定向到登录页,则登录
		if (curUrl.indexOf("/confluence/login.action") != -1) {
			driver.findElement(By.id("os_username")).sendKeys("kehui");
			driver.findElement(By.id("os_password")).sendKeys("kehui.1225");
			driver.findElement(By.id("loginButton")).click();
		}
		//点击创建按钮
		driver.findElement(By.id("create-page-button")).click();
		//点击生成默认的空白页
		driver.findElement(By.className("create-dialog-create-button")).click();
		Thread.sleep(1000l);
		//wiki标题
		driver.findElement(By.id("content-title")).sendKeys(title);
		//切换到wiki内容页,并填充标题
		driver.switchTo().frame("wysiwygTextarea_ifr");
		WebElement editor = driver.findElement(By.tagName("body"));
		JavascriptExecutor jsExecutor = (JavascriptExecutor) driver;
		jsExecutor.executeScript("document.body.innerHTML = '" + content + "';", editor);
		driver.switchTo().defaultContent();
		//driver.findElement(By.id("notifyWatchers")).click();
		//点击发布按钮
		//driver.findElement(By.id("rte-button-publish")).click();
	} catch (Exception e) {
		logger.error("exception-" + table.getTableName(), e);
	}
}
driver.close();
DBUtil.closeConn();

运行异常信息。

代码语言:javascript
复制
Caused by: org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with
Build info: version: '2.49.1', revision: '808c23b0963853d375cbe54b90bbd052e2528a54', time: '2016-01-21 09:37:52'
System info: host: 'DESKTOP-RHNIR30', ip: '10.7.242.80', os.name: 'Windows 8.1', os.arch: 'amd64', os.version: '6.3', java.version: '1.7.0_67'
Driver info: driver.version: unknown
	at <anonymous class>.fxdriver.preconditions.visible(file:///D:/apache-tomcat-7.0.64/temp/anonymous912190740134287098webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:10003)
	at <anonymous class>.DelayedCommand.prototype.checkPreconditions_(file:///D:/apache-tomcat-7.0.64/temp/anonymous912190740134287098webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12540)
	at <anonymous class>.DelayedCommand.prototype.executeInternal_/h(file:///D:/apache-tomcat-7.0.64/temp/anonymous912190740134287098webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12557)
	at <anonymous class>.DelayedCommand.prototype.executeInternal_(file:///D:/apache-tomcat-7.0.64/temp/anonymous912190740134287098webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12562)
	at <anonymous class>.DelayedCommand.prototype.execute/<(file:///D:/apache-tomcat-7.0.64/temp/anonymous912190740134287098webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12504)
2016-01-25 16:04:37,292 ERROR (com.manyi.iw.test.sth.service.impl.WikiServiceImpl:99) - exception-iw_sale_contract_customer_doc
org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with
Command duration or timeout: 16 milliseconds
Build info: version: '2.49.1', revision: '808c23b0963853d375cbe54b90bbd052e2528a54', time: '2016-01-21 09:37:52'
System info: host: 'DESKTOP-RHNIR30', ip: '10.7.242.80', os.name: 'Windows 8.1', os.arch: 'amd64', os.version: '6.3', java.version: '1.7.0_67'
Session ID: 413945ea-a8db-4b69-9f34-3868c540174d
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{platform=WINDOWS, acceptSslCerts=true, javascriptEnabled=true, cssSelectorsEnabled=true, databaseEnabled=true, browserName=firefox, handlesAlerts=true, nativeEvents=false, webStorageEnabled=true, rotatable=false, locationContextEnabled=true, applicationCacheEnabled=true, takesScreenshot=true, version=43.0.4}]
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
	at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206)
	at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
	at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:678)
	at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:327)
	at org.openqa.selenium.remote.RemoteWebElement.sendKeys(RemoteWebElement.java:122)
	at com.manyi.iw.test.sth.service.impl.WikiServiceImpl.autoPublishWiki(WikiServiceImpl.java:88)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:606)
	at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
	at com.manyi.iw.test.sth.interceptor.ExceptionInterceptor.invoke(ExceptionInterceptor.java:21)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
	at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)

异常并不总是发生,有些情况下。

当一个按钮点击一个新的页面,但是正在加载,chrome驱动找不到新的页面文档时,就会发生异常。

如何在没有firefoxdriver异常的情况下运行完成。请帮帮我。

EN

回答 2

Stack Overflow用户

发布于 2016-01-25 16:45:45

尝试对expected conditions使用显式等待

代码语言:javascript
复制
WebDriverWait wait = new WebDriverWait(driver, 20);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("id"));
element.click();

这将等待20秒以使元素可见。在此之后,元素将是可见的,并且您应该不会遇到与其交互的问题。

票数 1
EN

Stack Overflow用户

发布于 2016-05-10 22:11:06

对于ajax调用(在另一个应答线程中提问),如果在等待数据时显示加载指示器,则可以等待加载指示器不可见。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34988119

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档