selenium处理模态对话框

Selenium处理模态对话框

   

问题描述:

       点击按钮出现一个模态对话框,代码会卡在click这步不继续执行。原因是Selenium目前没有提供对模态对话框的处理。  

解决方案:

       将click出现弹出框这步用JS代替执行,然后切换到弹出窗就可以继续操作页面元素了。

测试地址:https://developer.mozilla.org/samples/domref/showModalDialog.html

代码如下:

public class junitTest {   

    WebDriver driver = new FirefoxDriver();

    String baseUrl = "https://developer.mozilla.org/samples/domref/showModalDialog.html";  

    @Test

    public void openModal() throws InterruptedException{

        driver.get(baseUrl);

        driver.findElement(By.xpath("/html/body/input")).click();    //点击后代码卡在这里

        Thread.sleep(2000);

        Set<String> handlers = driver.getWindowHandles();      

        for(String winHandler:handlers){           

            driver.switchTo().window(winHandler);                      

        }              

        driver.findElement(By.id("foo")).sendKeys("2");

        driver.findElement(By.xpath("/html/body/input[2]")).click();

    }

}

click这步替换为JS执行后代码:

public class junitTest {   

    WebDriver driver = new FirefoxDriver();

    String baseUrl = "https://developer.mozilla.org/samples/domref/showModalDialog.html";  

    @Test

    public void openModal() throws InterruptedException{

        driver.get(baseUrl);

        //driver.findElement(By.xpath("/html/body/input")).click();    //点击后代码卡在这里

        String js = "setTimeout(function(){document.getElementsByTagName('input')[0].click()},100)";       

        ((JavascriptExecutor)driver).executeScript(js);

        Thread.sleep(2000);

        Set<String> handlers = driver.getWindowHandles();      

        for(String winHandler:handlers){           

            driver.switchTo().window(winHandler);                          

        }              

        driver.findElement(By.id("foo")).sendKeys("2");

        driver.findElement(By.xpath("/html/body/input[2]")).click();

    }

}

转载于:https://www.cnblogs.com/chende/p/4468119.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值