【发布时间】: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","request":{"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