如何通过Python处理GhostDriver的警报? [英] How can I handle an alert with GhostDriver via Python?

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

问题描述

问题:
GhostDriver API尚不支持警报处理。暂时有一个可接受的解决方法,就是将自己的javascript注入到处理警报的页面中并为你存储文本。



我是通过python webdriver绑定使用此解决方法时遇到问题。这可能与我对新手级别的javascript理解有关。



以下是我尝试使用的解决方法示例:
https://github.com/detro/ghostdriver/issues/20#issuecomment-15641983



我正在使用一个公共站点来演示警报,使其更直接: http://www.tizag.com/javascriptT/javascriptalert.php



这是我的代码:



来自selenium import webdriver的$ pre class =lang-python prettyprint-override>

button_xpath =/ html / body / table [3 ] / tbody / tr / td [2] / table / tbody / tr / td / div [4] / form / input

js =
(function(){
var lastAlert = undefined;
window.alert = function(message){
lastAlert = message;
};
window.getLastAlert = function(){
var result = lastAlert;
lastAlert = undefined;
返回结果;
};
}());


driver = webdriver.PhantomJS()
driver.get('http://www.tizag.com/javascriptT/javascriptalert.php')
driver.execute_script(window.alert =%s%js)
driver.find_element_by_xpath(button_xpath).click()
#exception刚出现
driver.execute_script(返回窗口 .getLastAlert&& window.getLastAlert();)

例外是:

  WebDriverException:消息:u'Error消息=> \'点击失败:TypeError:\'undefined \'不是功能(评估\'alert(\'你确定要给我们你的房子吗?\')\')\'\ n由Request => {headers引起: { 接受: 应用/ JSON, 接受编码: 同一性, 连接: 关闭, 内容长度: 81, 内容类型:应用/ JSON;字符集= UTF-8\" , 主机: 127.0.0.1:41752\",\"User-Agent\":\"Python-urllib/2.7\"},\"httpVersion\":\"1.1\",\"method\":\"POST, 邮报: {\\ SE ssionId \\:\\0eaf7680-9897-11e2-b375-55b9cb6ceb0f \\,\\id \\:\\:wdc:1364578560610 \ \ }, URL: /点击, urlParsed:{ 锚: , 查询: , 文件: 点击, 目录: /, 路径: /点击, 相对: /点击, 口: , 主人: , 密码: , 用户: , 用户信息: , 权威: , 协议: , 源: /点击, queryKey:{}, 块:[ 点击]} urlOriginal:/ session / 0eaf7680-9897-11e2-b375-55b9cb6ceb0f / element /% 3Awdc% 3A1364578560610 / click};屏幕截图:可通过屏幕获得

我是JS noob。我希望有人可以指出我正确的方向。

解决方案

一个简单的解决方案是将window.alert方法重写为将参数输出到全局变量。



使用覆盖函数定义js注入变量:

  js =
window.alert = function(message){
lastAlert = message;
}

然后只需通过python中的execute_script调用传递js变量,如下所示:

  driver.execute_script(%s%js)

然后当它全部运行时你可以在全局lastAlert上执行一个返回:

  driver.execute_script(return lastAlert)


Problem: The GhostDriver API does not yet support alert handling. There is an acceptable workaround for the time being, which is to inject your own javascript into the page that will handle the alert and store it's text for you.

I'm having trouble using this workaround via the python webdriver bindings. It might be related to my novice level understanding of javascript.

Here is an example of the workaround I am trying to utilize: https://github.com/detro/ghostdriver/issues/20#issuecomment-15641983

I'm using a public site that demonstrates an alert to make this more straightforward: http://www.tizag.com/javascriptT/javascriptalert.php

Here is my code:

from selenium import webdriver

button_xpath = "/html/body/table[3]/tbody/tr/td[2]/table/tbody/tr/td/div[4]/form/input"

js = """
(function () {
var lastAlert = undefined;
window.alert = function (message) {
    lastAlert = message;
};
window.getLastAlert = function () {
    var result = lastAlert;
    lastAlert = undefined;
    return result;
};
}());
"""

driver = webdriver.PhantomJS()
driver.get('http://www.tizag.com/javascriptT/javascriptalert.php')
driver.execute_script("window.alert = %s" % js)
driver.find_element_by_xpath(button_xpath).click()
#exception just occured
driver.execute_script("return window.getLastAlert && window.getLastAlert();")

The exception is:

WebDriverException: Message: u'Error Message => \'Click failed: TypeError: \'undefined\' is not a function (evaluating \'alert(\'Are you sure you want to give us the deed to your house?\')\')\'\n caused by Request => {"headers":{"Accept":"application/json","Accept-Encoding":"identity","Connection":"close","Content-Length":"81","Content-Type":"application/json;charset=UTF-8","Host":"127.0.0.1:41752","User-Agent":"Python-urllib/2.7"},"httpVersion":"1.1","method":"POST","post":"{\\"sessionId\\": \\"0eaf7680-9897-11e2-b375-55b9cb6ceb0f\\", \\"id\\": \\":wdc:1364578560610\\"}","url":"/click","urlParsed":{"anchor":"","query":"","file":"click","directory":"/","path":"/click","relative":"/click","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/click","queryKey":{},"chunks":["click"]},"urlOriginal":"/session/0eaf7680-9897-11e2-b375-55b9cb6ceb0f/element/%3Awdc%3A1364578560610/click"}' ; Screenshot: available via screen 

I'm a JS noob. I'm hoping somebody can point me in the right direction.

解决方案

A simple solution is to rewrite the window.alert method to output the argument to a global variable.

Define the js injection variable with your override function:

js = """
window.alert = function(message) {
lastAlert = message;
}
"""

And then just pass the js variable through the execute_script call in python like this:

driver.execute_script("%s" % js)

Then when it's all been run you can execute a return on the global lastAlert:

driver.execute_script("return lastAlert")

这篇关于如何通过Python处理GhostDriver的警报?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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