Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Selenium 如何将手柄切换到具有确认窗口的弹出窗口_Selenium_Webdriver - Fatal编程技术网

Selenium 如何将手柄切换到具有确认窗口的弹出窗口

Selenium 如何将手柄切换到具有确认窗口的弹出窗口,selenium,webdriver,Selenium,Webdriver,看看我的问题 root.html单击链接 打开popup.html,同时打开确认窗口 我需要接受确认窗口,但无法切换到弹出窗口 我正在尝试使用SeleniumWebDriver生成测试代码。 一个测试用例,我需要接受确认窗口,但我不能。 我不酸,但selenium无法切换到已确认的窗口 ROOT.html 根窗口 函数orderpay(){ var type=“弹出”; document.orderpay.poptype.value=类型; paypop=window.open(“关于:空白”

看看我的问题

  • root.html单击链接
  • 打开popup.html,同时打开确认窗口
  • 我需要接受确认窗口,但无法切换到弹出窗口 我正在尝试使用SeleniumWebDriver生成测试代码。 一个测试用例,我需要接受确认窗口,但我不能。 我不酸,但selenium无法切换到已确认的窗口

    ROOT.html

    
    根窗口
    函数orderpay(){
    var type=“弹出”;
    document.orderpay.poptype.value=类型;
    paypop=window.open(“关于:空白”,“订单支付”,“宽度=620,高度=550,滚动条=yes”).focus();
    document.orderpay.submit();
    }
    
    POPUP.html

    
    弹出窗口
    函数showConfirm(){
    确认(“你能关闭我吗?”);
    警报(“这是警报窗口”);
    }
    你好,世界
    
    测试代码

    class ConfirmWindowTest {
        fun ifHasAlertClickOk(driver: WebDriver) {
            try {
                val alert = driver.switchTo().alert()
                alert.accept()
                sleep(1)
            } catch (e: Exception) {
                //e.printStackTrace()
            }
        }
    
        @Test
        fun closeConfirm() {
            var driver: WebDriver
            driver = ChromeDriver()
            driver.get("file:///{your file location}/root.html")
    
            //when this link click, popup.html will be opened
            driver.findElements(By.tagName("a")).first().click()
    
            //store root window handel
            val root = driver.windowHandle
    
            try {
                val handles = driver.windowHandles.filter { it != root }.toList()
                println(root)
                println(handles)
                handles.forEach {
                    //I want to switch popup window and close confirm and alert but it doesn't work.
                    driver.switchTo().window(it)
    
                    ifHasAlertClickOk(driver)
    
                    ifHasAlertClickOk(driver)
    
                    println("ok it works")
                }
            } catch (e: Exception) {
                e.printStackTrace()
            } finally {
                driver.switchTo().window(root)
            }
        }
    }
    
    现在它无法传递
    driver.switchTo().window(它)
    , 但我希望最后看到“ok it works”。

    您能尝试alert.Dismise()而不是alert.accept()?我总是使用“driver.switch_to.window(driver.window_handles[-1])”切换到Python中的新窗口(上次创建的)。