PhantomJSDriver接受警报 [英] PhantomJSDriver Accept Alert

查看:125
本文介绍了PhantomJSDriver接受警报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Java中接受使用PhantomJSDriver的警报?我正在尝试与YouTube合作。我无法让它工作。

How can I accept an alert with PhantomJSDriver in Java? I am trying to do this with YouTube. I can't get it to work.

我尝试使用此代码接受任何驱动程序,但它不适用于PhantomJS。

I've tried using this code to accept on any driver but it doesn't work with PhantomJS.

static void confirmDialog(WebDriver driver) {
    if (driver instanceof PhantomJSDriver) {
        PhantomJSDriver phantom = (PhantomJSDriver) driver;
        phantom.executeScript("window.confirm = function(){return true;}");
        phantom.executeScript("return window.confirm");
    } else driver.switchTo().alert().accept();
}


推荐答案

你必须执行JS来设置window.alert调用什么都不做。您可以使用此方法。

You must execute JS to set the window.alert call to do nothing. You can use this method.

static void confirmDialog(WebDriver driver) {
    if (driver instanceof PhantomJSDriver) {
        PhantomJSDriver phantom = (PhantomJSDriver) driver;
        phantom.executeScript("window.alert = function(){}");
        phantom.executeScript("window.confirm = function(){return true;}");
    } else driver.switchTo().alert().accept();
}

这篇关于PhantomJSDriver接受警报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆