Selenium switch focus to tab, which opened after clicking link

后端 未结 3 1420
小鲜肉
小鲜肉 2020-11-27 08:44

For automation purposes, I am working on creating a script that finds a row in a table. This row is clickable and opens a new tab/adress.

With selenium, I am now ab

3条回答
  •  执念已碎
    2020-11-27 09:14

     **Try this code:**
    
       public static void switchToNewWindow(WebDriver driver, WebElement 
       causeOfNewWindow) {
    
        // Get Handles before opening new window
        Set oldHandles = driver.getWindowHandles();
    
        // Click element to open new Window
        causeOfNewWindow.click();
    
        // Get Handles after opening new window
        Set newHandles = driver.getWindowHandles();
    
        for (String handle : newHandles) {
    
            if (!oldHandles.contains(handle)) {
                driver.switchTo().window(handle);
            }
    
        }
    }
    

提交回复
热议问题