有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Selenium>Webdriver>Java>单击模型窗口中的按钮时,另一个后台按钮正在触发

在我的结帐页面上,对于第三方支付提供商,在模型窗口中,我可以使用xpath&;只显示一个按钮。但是,当我的自动化脚本运行时,它会单击另一个按钮,该按钮不在“模型”窗口中,但与所需按钮位于同一位置,但在“模型”窗口的背景中。任何建议


共 (1) 个答案

  1. # 1 楼答案

    Have you added the code to switch to this model window before clicking on the button?
    
    
     public void waitForNewWindowAndSwitchToIt(WebDriver driver) throws InterruptedException {
                    String cHandle = driver.getWindowHandle();           
                    String newWindowHandle = null;
                    Set<String> allWindowHandles = driver.getWindowHandles();
    
                    //Wait for 20 seconds for the new window and throw exception if not found
                    for (int i = 0; i < 20; i++) {
                        if (allWindowHandles.size() > 1) {
                            for (String allHandlers : allWindowHandles) {
                                if (!allHandlers.equals(cHandle))
                                    newWindowHandle = allHandlers;                          
                            }
                            driver.switchTo().window(newWindowHandle);
                            break;
                        } else {
                            Thread.sleep(1000);
                        }
                    }
                    if (cHandle == newWindowHandle) {
                        throw new RuntimeException(
                                "Time out - No window found");
                    }
                }