Selenium switch focus to tab, which opened after clicking link

后端 未结 3 1422
小鲜肉
小鲜肉 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:08

    Maybe you just have to wait until a second window is created? Maybe selenium checks window handles too fast?

    Try with WebDriverWait

    Example:

    String currentHandle = driver.getWindowHandle();
    WebDriverWait wait = new WebDriverWait(driver, 10);
    wait.until(ExpectedConditions.numberOfWindowsToBe(2));
    
    Set allHandles = driver.getWindowHandles();
    for (String handle : allHandles) {
        if (!handle.equals(currentHandle)) driver.switchTo().window(handle);
    }
    

    If a number of windows will be less or more than 2, TimeoutException will occur.

提交回复
热议问题