Switch tabs using Selenium WebDriver with Java

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

    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.
    

提交回复
热议问题