使用带有 Java 的 Selenium WebDriver 在浏览器选项卡之间切换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25525209/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 00:34:48  来源:igfitidea点击:

Switch between browser tabs using Selenium WebDriver with Java

javaseleniumselenium-webdriverbrowser-tab

提问by mahi

I need to switch between the browser tabs, used the following code,

我需要在浏览器选项卡之间切换,使用以下代码,

driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"\t");

It was working properly sometimes only, but sometimes it is showing an exception. Can anyone suggest me is there any other instructions for switching tabs within a single window by using java.

它有时只能正常工作,但有时却显示异常。任何人都可以建议我是否有任何其他说明可以使用 java.lang.

采纳答案by Fahim Hossain

You have to use window handle function here. Here is a sample working code in java:

你必须在这里使用窗口句柄函数。这是 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

Hope this helps!

希望这可以帮助!

回答by nitesh

Switching between browser window is different from switching b/w tabs.

在浏览器窗口之间切换不同于切换黑白选项卡。

In some browser windowhandler command may work but it wont work in all browser.

在某些浏览器中,windowhandler 命令可能有效,但不适用于所有浏览器。

Here is the solution to navigate b/w tabs

这是导航黑白选项卡的解决方案

for navigating left to right side:

用于从左到右侧导航:

Actions action= new Actions(driver);
action.keyDown(Keys.CONTROL).sendKeys(Keys.TAB).build().perform();

For navigating right to left :

从右到左导航:

Actions action= new Actions(driver);
action.keyDown(Keys.CONTROL).keyDown(Keys.SHIFT).sendKeys(Keys.TAB).build().perform();

回答by Shivam Bharadwaj

In my case, the following code is working fine-

就我而言,以下代码工作正常-

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);