硒所需的功能-设置PhantomJS驱动程序的handlesAlerts [英] Selenium Desired Capabilities - set handlesAlerts for PhantomJS driver

查看:70
本文介绍了硒所需的功能-设置PhantomJS驱动程序的handlesAlerts的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用webdriver试用phantomJS,但在处理JavaScript警报时遇到了麻烦.我注意到phantomjs驱动程序desired_capabilities有一个字段'handlesAlerts': False,有没有办法将此值设置为true?我尝试了显而易见的方法,但是没有任何效果:

I'm trying out phantomJS with webdriver and I'm having trouble with handling javascript alerts. I notice the phantomjs driver desired_capabilities has a field 'handlesAlerts': False Is there a way to set this value to true? I've tried the obvious way but that doesn't have any effect:

drv = webdriver.PhantomJS(desired_capabilities={'handlesAlerts': True})

print drv.desired_capabilities

{u'browserName': u'phantomjs',
 u'driverName': u'ghostdriver',
 u'driverVersion': u'1.0.3',
 u'handlesAlerts': False,
 u'javascriptEnabled': True,...}

我可以更改字典drv.desired_capabilities['handlesAlerts'] = True中的值,但是当我尝试切换到警报时,会收到错误消息.

I can change the value in the dictionary drv.desired_capabilities['handlesAlerts'] = True, but when I try to switch to an alert, I get an error message.

$cat index.html 
<html>
<body>
<script type="text/javascript">
    alert('FOO!');
</script>
    Hello World.
</body>
</html>

>>> from selenium import webdriver
>>> driver = webdriver.PhantomJS()
>>> driver.desired_capabilities['handlesAlerts'] = True
>>> driver.get('index.html')
>>> alert = driver.switch_to_alert()
>>> alert.text

Traceback (most recent call last):
<snip>
selenium.common.exceptions.WebDriverException: Message: 
   'Invalid Command Method -  Request    => 
                 {"headers":{"Accept":"application/json",
                              "Accept- Encoding":"identity",
                              "Connection":"close",
                              "Content-Type":"application/json;charset=UTF- 8",
                              "Host":"127.0.0.1:56009", 
                              "User-Agent":"Python- urllib/2.7"},
                  "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/cd31ed90-a5f8-11e2-856d-5783db9f5342/alert_text"}' 

推荐答案

API指定将所需功能传递给构造函数.但是,可能是驱动程序不支持所需功能中要求的功能的情况.在那种情况下,驱动程序不会抛出任何错误,这是有意的.会话返回一个功能对象,该对象指示该会话实际支持的功能.

The API specifies that desired capabilities be passed into the constructor. However, it may be the case that a driver does not support a feature requested in the desired capabilities. In that case, no error is thrown by the driver, and this is intentional. A capabilities object is returned by the session which indicates the capabilities that the session actually supports.

在这种情况下,这就是实际发生的情况. PhantomJS驱动程序不支持处理警报,如源代码中所示,返回的功能对象表示的内容相同.在大多数语言绑定中,此返回的功能对象是只读的.在返回的对象可能是读写的语言绑定中,修改这些功能对会话没有实际影响.在待处理的 W3C WebDriver规范中,有一个requiredCapabilities设置,如果该设置会抛出异常服务器无法提供该功能,但据我所知尚未由任何驱动程序实现.

That is what's actually happening in this case. The PhantomJS driver doesn't support handling alerts as seen in the source code, the returned capabilities object indicates as much. In most language bindings, this returned capabilities object is read-only; in language bindings where the returned object may be read-write, modifying these capabilities has no actual effect on the session. In the pending W3C WebDriver specification, there is a requiredCapabilities setting which would throw an exception if the server cannot provide the capability, but that hasn't been implemented by any driver yet as far as I'm aware.

这篇关于硒所需的功能-设置PhantomJS驱动程序的handlesAlerts的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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