简体   繁体   English

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

[英]Switch between browser tabs using Selenium WebDriver with Java

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.

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!希望这可以帮助!

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Selenium WebDriver 和 Java 切换选项卡 - Switch tabs using Selenium WebDriver with Java 无法使用Selenium WebDriver在两个浏览器窗口之间切换 - Unable to switch between two browser windows using Selenium WebDriver 如何在 java 中使用 selenium 在两个或多个 chrome 浏览器 windows(不是标签)之间切换? - How to switch between two or mutlple chrome browser windows (Not tabs) using selenium in java? 如何使用Java在Selenium WebDriver中切换帧 - How to switch between frames in Selenium WebDriver using Java 如何使用带有Java的Selenium WebDriver在Chrome中的Windows之间切换? - How to switch between windows in Chrome using Selenium WebDriver with Java? 如何使用Java在Selenium Webdriver中的2个浏览器之间切换 - How to switch between 2 browsers in selenium webdriver with java 如何使用Selenium Webdriver在同一窗口中打开多个选项卡(超过2个)之间切换 - How to switch between multiple tabs(more than 2) open in same window using Selenium Webdriver 如何打开多个选项卡并通过Selenium和Webdriver在之间切换? - How to open multiple tabs and switch between through Selenium and 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
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM