Switch tabs using Selenium WebDriver with Java

前端 未结 21 1178
野性不改
野性不改 2020-11-22 12:09

Using Selenium WebDriver with JAVA. I am trying to automate a functionality where I have to open a new tab do some operations there and come back to previous tab (Parent). I

相关标签:
21条回答
  • 2020-11-22 12:41

    The first thing you need to do is opening a new tab and save it's handle name. It will be best to do it using javascript and not keys(ctrl+t) since keys aren't always available on automation servers. example:

    public static String openNewTab(String url) {
        executeJavaScript("window.parent = window.open('parent');");
        ArrayList<String> tabs = new ArrayList<String>(bot.driver.getWindowHandles());
        String handleName = tabs.get(1);
        bot.driver.switchTo().window(handleName);
        System.setProperty("current.window.handle", handleName);
        bot.driver.get(url);
        return handleName;
    }
    

    The second thing you need to do is switching between the tabs. Doing it by switch window handles only, will not always work since the tab you'll work on, won't always be in focus and Selenium will fail from time to time. As I said, it's a bit problematic to use keys, and javascript doesn't really support switching tabs, so I used alerts to switch tabs and it worked like a charm:

    public static void switchTab(int tabNumber, String handleName) {
            driver.switchTo().window(handleName);
            System.setProperty("current.window.handle", handleName);
            if (tabNumber==1)
                executeJavaScript("alert(\"alert\");");
            else
                executeJavaScript("parent.alert(\"alert\");");
            bot.wait(1000);
            driver.switchTo().alert().accept();
        }
    
    0 讨论(0)
  • 2020-11-22 12:41

    It is A very simple process: assume you have two tabs so you need to first close the current tab by using client.window(callback) because the switch command "switches to the first available one". Then you can easily switch tab using client.switchTab.

    0 讨论(0)
  • 2020-11-22 12:44

    Since the driver.window_handles is not in order , a better solution is this.

    first switch to the first tab using the shortcut Control + X to switch to the 'x' th tab in the browser window .

    driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "1");
    # goes to 1st tab
    
    driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "4");
    # goes to 4th tab if its exists or goes to last tab.
    
    0 讨论(0)
  • 2020-11-22 12:44

    A brief example of how to switch between tabs in a browser (in case with one window):

    // open the first tab
    driver.get("https://www.google.com");
    Thread.sleep(2000);
    
    // open the second tab
    driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "t");
    driver.get("https://www.google.com");
    Thread.sleep(2000);
    
    // switch to the previous tab
    driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "" + Keys.SHIFT + "" + Keys.TAB);
    Thread.sleep(2000);
    

    I write Thread.sleep(2000) just to have a timeout to see switching between the tabs.

    You can use CTRL+TAB for switching to the next tab and CTRL+SHIFT+TAB for switching to the previous tab.

    0 讨论(0)
  • 2020-11-22 12:46

    This is a simple solution for opening a new tab, changing focus to it, closing the tab and return focus to the old/original tab:

    @Test
    public void testTabs() {
        driver.get("https://business.twitter.com/start-advertising");
        assertStartAdvertising();
    
        // considering that there is only one tab opened in that point.
        String oldTab = driver.getWindowHandle();
        driver.findElement(By.linkText("Twitter Advertising Blog")).click();
        ArrayList<String> newTab = new ArrayList<String>(driver.getWindowHandles());
        newTab.remove(oldTab);
        // change focus to new tab
        driver.switchTo().window(newTab.get(0));
        assertAdvertisingBlog();
    
        // Do what you want here, you are in the new tab
    
        driver.close();
        // change focus back to old tab
        driver.switchTo().window(oldTab);
        assertStartAdvertising();
    
        // Do what you want here, you are in the old tab
    }
    
    private void assertStartAdvertising() {
        assertEquals("Start Advertising | Twitter for Business", driver.getTitle());
    }
    
    private void assertAdvertisingBlog() {
        assertEquals("Twitter Advertising", driver.getTitle());
    }
    
    0 讨论(0)
  • 2020-11-22 12:48

    This will work for the MacOS for Firefox and Chrome:

    // opens the default browser tab with the first webpage
    driver.get("the url 1");
    thread.sleep(2000);
    
    // opens the second tab
    driver.findElement(By.cssSelector("Body")).sendKeys(Keys.COMMAND + "t");
    driver.get("the url 2");
    Thread.sleep(2000);
    
    // comes back to the first tab
    driver.findElement(By.cssSelector("Body")).sendKeys(Keys.COMMAND, Keys.SHIFT, "{");
    
    0 讨论(0)
提交回复
热议问题