java - Selenium>Webdriver>Java>单击模型窗口中的按钮时,触发另一个背景按钮

标签 java selenium model webdriver

在我的结帐页面上,对于第 3 方支付提供商,在模型窗口中,我可以使用 xpath 找到所需的按钮,并且仅显示一个按钮。但是,当我的自动化脚本运行时,它会单击另一个按钮,该按钮不在模型窗口中,但与我所需的按钮位置相同,但在模型窗口的背景中。有什么建议吗?

最佳答案

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");
                }
            }

关于java - Selenium>Webdriver>Java>单击模型窗口中的按钮时,触发另一个背景按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60767849/

相关文章:

java - 为什么我收到 AttachFile(File) is undefined for the type MimeBodyPart?

java - 在 ANTLR4 中运行时更改与节点关联的规则

java - 即发即忘与 http 请求相比

mysql - 无法使用 Controller 中的创建功能在数据库中创建条目

Laravel 关系 一到两列

java - 三角形质心代码

java - 带有 JUnit 测试的 Log4J

java - 使用 xpath 轴在 Selenium 中遍历 DOM

javascript - 如何在 python 中使用 selenium 驱动程序单击链接而不知道 anchor 标记内的文本

express - 使用 Node Express 路由后数据存储在错误的表中