[英]Switch between browser tabs using Selenium WebDriver with Java
我需要在浏览器选项卡之间切换,使用以下代码,
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"\t");
它有时只能正常工作,但有时却显示异常。 任何人都可以建议我是否有任何其他说明可以使用 java.lang.
你必须在这里使用窗口句柄函数。 这是 Java 中的示例工作代码:
String parentHandle = driver.getWindowHandle(); // get the current window handle
System.out.println(parentHandle); //Prints the parent window handle
String anchorURL = anchor.getAttribute("href"); //Assuming u are clicking on a link which opens a new browser window
anchor.click(); //Clicking on this window
for (String winHandle : driver.getWindowHandles()) { //Gets the new window handle
System.out.println(winHandle);
driver.switchTo().window(winHandle); // switch focus of WebDriver to the next found window handle (that's your newly opened window)
}
//Now your driver works on the current new handle
//Do some work here.....
//Time to go back to parent window
driver.close(); // close newly opened window when done with it
driver.switchTo().window(parentHandle); // switch back to the original window
希望这可以帮助!
在浏览器窗口之间切换不同于切换黑白选项卡。
在某些浏览器中,windowhandler 命令可能有效,但不适用于所有浏览器。
这是导航黑白选项卡的解决方案
用于从左到右侧导航:
Actions action= new Actions(driver);
action.keyDown(Keys.CONTROL).sendKeys(Keys.TAB).build().perform();
从右到左导航:
Actions action= new Actions(driver);
action.keyDown(Keys.CONTROL).keyDown(Keys.SHIFT).sendKeys(Keys.TAB).build().perform();
就我而言,以下代码工作正常-
String oldTab=driver.getWindowHandle();
driver.findElement(pageObj.getL_Popup_Window()).click();
ArrayList<String> newTab = new ArrayList<String>(driver.getWindowHandles());
newTab.remove(oldTab);
driver.switchTo().window(newTab.get(0));
WebElement ele = driver.findElement(pageObj.getI_input_name());
ele.click();
ele.sendKeys(name);
driver.findElement(pageObj.getI_submit()).click();
driver.switchTo().window(oldTab);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.