Switch tabs using Selenium WebDriver with Java

前端 未结 21 1587
野性不改
野性不改 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: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.

提交回复
热议问题