【问题标题】:Accept a Popup using Phantomjs with Selenium使用带有 Selenium 的 Phantomjs 接受弹出窗口
【发布时间】:2016-11-16 04:35:50
【问题描述】:

我正在使用 python 2.7、Selenium 和 PhantomJs。

当我单击脚本中的登录按钮时,它会转到一个新页面并出现一个我必须接受的弹出窗口。

这是我尝试使用的代码。我从这里得到的 How can I handle an alert with GhostDriver via Python?

sign_in.click() 
js = 'window.alert = function(message) { lastAlert = message; }'
driver.execute_script("%s" % js) 
driver.execute_script("return lastAlert")

这是我得到的错误:

引发异常类(消息、屏幕、堆栈跟踪) selenium.common.exceptions.WebDriverException:消息: {"errorMessage":"找不到变量:lastAlert","re​​quest":{"hea ders":{"Accept":"application/json","Accept-Encoding":"identity","Connection":"close","Content-Length":"95","Content-Type ":"application/json;charset=UTF-8","Host":"127.0.0.1:56712","User-Agent":"Python-urllib/2.7"},"httpVersion":"1.1","方法 d":"POST","post":"{\"sessionId\": \"3832e2c0-4902-11e6-b766-0d7f487d0794\", \"args\": [], \"script\": \"返回最后一个警报 \"}","url":"/execute","urlParsed":{"anchor":"","query":"","file":"execute","directory":"/","路径“:”/执行“,”相对“: "/execute","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol" :"","来源":"/执行","q ueryKey":{},"chunks":["execute"]},"urlOriginal":"/session/3832e2c0-4902-11e6-b766-0d7f487d0794/execute"}} 屏幕截图:可通过屏幕获得

我对 javascript 的经验不是很丰富,如果找到正确的方向会很有用。

【问题讨论】:

    标签: python-2.7 selenium phantomjs


    【解决方案1】:

    这是我正在使用的 Behat 步骤:

    /** 
      * This step overrides windows.confirm and basically accepts it before it is displayed.
      *
      * @When /^(?:|I )bypass the popup$/
      */
      public function bypassPopup() {
        $function = "
        var realConfirm=window.confirm;
        window.confirm=function(){
        window.confirm=realConfirm;
        return true;
    };
        ";
        $session = $this->getSession();
        $session->executeScript($function);
      }
    

    您在点击按钮之前放置它,当您点击您的弹出窗口时将被自动接受。 在这种情况下,只会接受第一个弹出窗口。

    【讨论】:

      猜你喜欢
      • 2021-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-14
      • 1970-01-01
      • 1970-01-01
      • 2021-05-19
      相关资源
      最近更新 更多