先看一下,从selenium IDE 导出的测试代码
package com.test;
import static org.junit.Assert.fail;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
public class JustForCSDN {
private WebDriver driver;
private String baseUrl;
private StringBuffer verificationErrors = new StringBuffer();
@Before
public void setUp() throws Exception {
driver = new FirefoxDriver();
baseUrl = "http://www.yourSite.com/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@Test
public void testJustForCSDN() throws Exception {
driver.get(baseUrl + "/");
driver.findElement(By.id("user_name")).clear();
driver.findElement(By.id("user_name")).sendKeys("test");
driver.findElement(By.cssSelector("div.loginbtn")).click();
driver.findElement(By.id("password")).clear();
driver.findElement(By.id("password")).sendKeys("test");
driver.findElement(By.id("myBuild_list")).click();
driver.findElement(By.xpath("//div[@onclick='add_build()']")).click();
// ERROR: Caught exception [ERROR: Unsupported command [waitForPopUp | | 30000]]
driver.findElement(By.id("xmjl")).clear();
driver.findElement(By.id("xmjl")).sendKeys("RindyRRRRR");
driver.findElement(By.id("abb")).clear();
driver.findElement(By.id("abb")).sendKeys("RRRR");
new Select(driver.findElement(By.id("area")))
.selectByVisibleText("闸北区");
driver.findElement(By.id("detail_address")).clear();
driver.findElement(By.id("detail_address")).sendKeys("xxxx");
}
@After
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}
}
中间有段被注释了的error :
**// ERROR: Caught exception [ERROR: Unsupported command [waitForPopUp | | 30000]]**
我以为是这个新打开的页面加载的时间太慢了,selenium 执行太快,所以检测不到这个页面上的一些元素,所以每次运行junit 都会报错
org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"id","selector":"user_name"}
Command duration or timeout: 30.57 seconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.52.0', revision: '4c2593cfc3689a7fcd7be52549167e5ccc93ad28', time: '2016-02-11 11:22:43'
System info: host: 'RindyPC', ip: '10.0.1.122', os.name: 'Windows 8', os.arch: 'x86', os.version: '6.2', java.version: '1.7.0_13'
*** Element info: {Using=id, value=user_name}
Session ID: 54bb886b-39a5-4b54-8131-90b7719ac284
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=39.0.3}]
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:525)
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.RemoteWebDriver.findElement(RemoteWebDriver.java:363)
at org.openqa.selenium.remote.RemoteWebDriver.findElementById(RemoteWebDriver.java:413)
at org.openqa.selenium.By$ById.findElement(By.java:218)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:355)
at com.dev.fangxinjia.JustForCSDN.testJustForCSDN(JustForCSDN.java:30)
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:601)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"id","selector":"user_name"}
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.52.0', revision: '4c2593cfc3689a7fcd7be52549167e5ccc93ad28', time: '2016-02-11 11:22:43'
System info: host: 'RindyPC', ip: '10.0.1.122', os.name: 'Windows 8', os.arch: 'x86', os.version: '6.2', java.version: '1.7.0_13'
Driver info: driver.version: unknown
at .FirefoxDriver.prototype.findElementInternal_(file:///C:/Users/Rindy/AppData/Local/Temp/anonymous7502933304520874302webdriver-profile/extensions/[email protected]/components/driver-component.js:10723)
at .fxdriver.Timer.prototype.setTimeout/<.notify(file:///C:/Users/Rindy/AppData/Local/Temp/anonymous7502933304520874302webdriver-profile/extensions/[email protected]/components/driver-component.js:623)
一大段报错,弄得人都烦了。
然后各种根据trace去百度解决方案,加上各种看selenium- webdriver的文档,之前就是看了一点小知识就开始照着网上一些前辈写的例子练习,没有仔细看过,没想到测试网站的时候,就各种报错了,还是要拿着文档好好看啊。修改后的代码如下:
package com.test;
import static org.junit.Assert.fail;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
public class JustForCSDN {
private WebDriver driver;
private String baseUrl;
private StringBuffer verificationErrors = new StringBuffer();
@Before
public void setUp() throws Exception {
driver = new FirefoxDriver();
baseUrl = "http://www.yourSite.com/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@Test
public void testJustForCSDN() throws Exception {
driver.get(baseUrl + "/");
driver.findElement(By.id("user_name")).clear();
driver.findElement(By.id("user_name")).sendKeys("test");
driver.findElement(By.cssSelector("div.loginbtn")).click();
driver.findElement(By.id("password")).clear();
driver.findElement(By.id("password")).sendKeys("test");
driver.findElement(By.id("myBuild_list")).click();
driver.findElement(By.xpath("//div[@onclick='add_build()']")).click();
// ERROR: Caught exception [ERROR: Unsupported command [waitForPopUp | | 30000]]
String currentWindow = driver.getWindowHandle();// 获取当前窗口句柄
Set handles = driver.getWindowHandles();// 获取所有窗口句柄
Iterator it = handles.iterator();
while (it.hasNext()) {
if (currentWindow == it.next()) {
continue;
}
driver = driver.switchTo().window(it.next());// 切换到新窗口
//添加工地
driver.findElement(By.id("xmjl")).clear();
driver.findElement(By.id("xmjl")).sendKeys("RindyRRRRR");
driver.findElement(By.id("abb")).clear();
driver.findElement(By.id("abb")).sendKeys("RRRR");
new Select(driver.findElement(By.id("area")))
.selectByVisibleText("闸北区");
driver.findElement(By.id("detail_address")).clear();
driver.findElement(By.id("detail_address")).sendKeys("xxxx");
}
//driver.switchTo().window(currentWindow);//回到原来页面
}
@After
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}
}
这里放上前辈的解释:
**在上面的代码里,使用 windowhandle 方法来获取当前浏览器窗口的句柄,使用了
windowhandles 方法获取所有弹出的浏览器窗口的句柄,然后通过排除当前句柄的
方法来得到新开窗口的句柄。
在获取新弹出窗口的句柄后,使用 switchto.window(newwindow_handle)方法,将新
窗口的句柄作为参数传入既可捕获到新窗口了。**
好了,如果你也遇到这个问题,赶紧去试试解决办法吧