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; }");        

        

转载于:https://www.cnblogs.com/liuyunbuji/p/7873985.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值