python selenium+phantomjs alert()弹窗报错

 问题:用selenium+phantomjs 模拟登陆,网页用JavaScript的alert(“登陆成功”)弹出框,但是用switch_to_alert().accept()报错,不可执行命令。

目标代码:<script language="javascript">alert('********************');</script>

显示错误:

File "sy.py", line 112, in <module>
Pop123= Pop_up.text
File "C:\Python27\lib\site-packages\selenium\webdriver\common\alert.py", line 69, in text
return self.driver.execute(Command.GET_ALERT_TEXT)["value"]
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 308, in execute
self.error_handler.check_response(response)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 165, in check_response
raise exception_class(value)
selenium.common.exceptions.WebDriverException: Message: Invalid Command Method - {"headers":{"Accept":"application/json","Accept-Encoding":"identity","Connection":"close","Content-Type":"application/json;charset=UTF-8","Host":"127.0.0.1:59198","User-Agent":"Python http auth"},"httpVersion":"1.1","method":"GET","url":"/alert_text","urlParsed":{"anchor":"","query":"","file":"alert_text","directory":"/","path":"/alert_text","relative":"/alert_text","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/alert_text","queryKey":{},"chunks":["alert_text"]},"urlOriginal":"/session/95d315d0-ce94-11e7-8d72-c1b9396af0ea/alert_text"}

搜到的解决方案:

   phantomjs是没有界面的,无法j解析动态的JS,所以对alert也是无法直接弹出的,故phantomjs以该函数回调在page在执行过程中的alert事件。

  python没有对应phentomjs特殊的库,所以需要调用JS。

  python selenium官方已不再对phentomjs提供支持。现在主打火狐浏览器 和 谷歌浏览器 请尽量更换

 

一 、python调用Js

 

  r = driver.execute_script("return newsJason")

 

  print r

二、Js里面的方法          时间较忙未进行测试对错

  page.onAlert = function(msg) {}

  driver.execute_script("window.confirm = function(msg) { return true; }");        

        

posted @ 2017-11-21 16:43  流云不羁  阅读(1375)  评论(0编辑  收藏  举报