[英]Selenium web driver | java | unable to switch between firefox browser windows
Using Selenium Webdriver 2. java. 使用Selenium Webdriver2。java。
I would like to switch back in forth between two firefox browser windows. 我想在两个Firefox浏览器窗口之间来回切换。 When I do I get: org.openqa.selenium.NoSuchWindoException: Unable to loacate window"{accb1cc2-74c9-3b4e-8f71-c0b184a037c4}";
当我得到时:org.openqa.selenium.NoSuchWindoException:无法定位窗口“ {accb1cc2-74c9-3b4e-8f71-c0b184a037c4}”; duration or timeout:
持续时间或超时:
Here is the java: 这是java:
driver = new FirefoxDriver();
driver.get("http://mail.google.com");
String firstWindowHandle = driver.getWindowHandle();
System.out.println("handle of first window ="+firstWindowHandle);
Thread.sleep(1000);
driver = new FirefoxDriver();
driver.get("http://www.google.com");
// Get names of currently open windows
String secondWindowHandle = driver.getWindowHandle();
System.out.println("handle of first window ="+secondWindowHandle);
Thread.sleep(1000);
// It fails right here!
driver.switchTo().window(firstWindowHandle );
driver.get("http://www.lifehacker.com");
It prints the following to the console: - handle of first window = {accb1cc2-74c9-3b4e-8f71-c0b184a037c4} - handle of the second window = {f5256619-a36e-a441-9979-937da0abacd1} 它将以下内容打印到控制台:-第一个窗口的句柄= {accb1cc2-74c9-3b4e-8f71-c0b184a037c4}-第二个窗口的句柄= {f5256619-a36e-a441-9979-937da0abacd1}
All help is appreciated. 感谢所有帮助。
Unfortunately, you cannot switch between windows the way you are currently trying to do it - WebDriver lost the first window as soon as you instantiated a new instance. 不幸的是,您无法以当前尝试的方式在窗口之间切换-实例化新实例后,WebDriver会丢失第一个窗口。
You could try opening the second window via javascript and then switching back and forth from it: 您可以尝试通过javascript打开第二个窗口,然后从中来回切换:
window.open('http://www.bing.com','Bing','modal=yes,alwaysRaised=yes')
This is a bit of a hack, and could have the following problems: 这有点hack,可能会出现以下问题:
Some final thoughts: 最后的一些想法:
Is there any particular reason it has to be the same driver instance? 是否必须具有相同的驱动程序实例,是否有任何特殊原因? If not, just switch between two driver instances:
如果没有,只需在两个驱动程序实例之间切换:
FirefoxDriver driver = new FirefoxDriver();
driver.get("http://mail.google.com");
FirefoxDriver driver2 = new FirefoxDriver();
driver2.get("http://www.google.com");
Swtiching between 2 active Windows: 在2个活动的Windows之间切换:
FirefoxDriver wd=new FirefoxDriver();
wd.get("https://irctc.co.in/");
wd.manage().timeouts().implicitlyWait(5000,TimeUnit.SECONDS);
WebElement wb=wd.findElement(By.linkText("Cabs"));
wb.click(); //Now 2 Windows are open
wd.manage().timeouts().implicitlyWait(5000,TimeUnit.SECONDS); //Wait for the complete page to load
Set<String> sid=wd.getWindowHandles(); //getWindowHandles() method returns the ids of all active Windows and its return type will be a Collection Set.
Iterator<String> it=sid.iterator(); //Using iterator we can fetch the values from Set.
String parentId=it.next();
System.out.println(parentId);
String childId=it.next();
System.out.println(childId);
wd.switchTo().window(childId); //swtiching control to child Window
wd.close(); wd.close(); //control returns to the parent Window.
//控制返回到父窗口。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.