简体   繁体   English

Selenium Web驱动程序| Java | 无法在Firefox浏览器窗口之间切换

[英]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,可能会出现以下问题:

  • Popup blockers may prevent the action 弹出窗口阻止程序可能会阻止该操作
  • The browser must have javascript enabled 浏览器必须启用了javascript
  • Future browser versions may break the hack 未来的浏览器版本可能会破解黑客
  • Complaining and murmuring from peers (and perhaps rightly so) because even though it might work, it's still a hack ;) 向同行抱怨和抱怨(也许是对的),因为即使它可能起作用,它仍然是一个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.

相关问题 Selenium Web驱动程序-无法切换到框架(Java) - Selenium Web Driver - Unable switch to frame (Java) selenium web driver:能够打开Firefox浏览器,但无法加载Url - selenium web driver : Able to open Firefox browser but unable to load Url 无法使用Selenium WebDriver在两个浏览器窗口之间切换 - Unable to switch between two browser windows using Selenium WebDriver 使用Selenium Java在浏览器中的两个窗口之间切换 - switch between two windows in browser using Selenium java 如何使用Selenium java在浏览器中的两个窗口之间切换 - How to switch between two windows in browser using Selenium java 如何将硒Web驱动程序用于所有浏览器-chrome,safari,firefox,IE(JAVA) - How to use selenium web driver for all browser-chrome,safari,firefox,IE(JAVA) Java-无法在Firefox浏览器中使用Selenium网格 - Java - Unable to use selenium grid with firefox browser 无法使用Selenium Web Driver 2.33和Firefox 22打开网页 - Unable to open webpage using selenium web driver 2.33 and firefox 22 Selenium Web-Driver Firefox Profile - 禁用弹出窗口和警报窗口 - Selenium Web-Driver Firefox Profile - Disable popup and alert windows 如何在 java 中使用 selenium 在两个或多个 chrome 浏览器 windows(不是标签)之间切换? - How to switch between two or mutlple chrome browser windows (Not tabs) using selenium in java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM