How do I change focus to a new popup tab in Selenium?

后端 未结 7 1328
渐次进展
渐次进展 2020-12-29 06:38

I\'m using Selenium and Firefox.

I have a link on a page (say linkA) that opens a new page in a new tab. The new tab is displayed when linkA is clicked. I then w

7条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-29 06:41

    Simply use this code.

    public void newtab(){
    
        System.setProperty("webdriver.chrome.driver", "E:\\eclipse\\chromeDriver.exe");
    
        WebDriver driver = new ChromeDriver();
    
        driver.get("http://www.w3schools.com/tags/att_a_target.asp");
    
        //I have provided a sample link. Make sure that you have provided the correct link in the above line.
    
        driver.findElement(By.className("tryitbtn")).click();
    
        new Actions(driver).sendKeys(driver.findElement(By.tagName("html")), Keys.CONTROL).sendKeys(driver.findElement(By.tagName("html")), Keys.NUMPAD2).build().perform();
    
    
        // In keyboard we will press 
    
        //ctrl+1 for 1st tab
    
       //ctrl+2 for 2nd tab
    
       //ctrl+3 for 3rd tab.
    
      //Same action is written in the above code.
    
    }
    

提交回复
热议问题