【问题标题】:Selenium Desired Capabilities - set handlesAlerts for PhantomJS driverSelenium Desired Capabilities - 为 PhantomJS 驱动程序设置句柄警报
【发布时间】:2013-04-07 23:36:00
【问题描述】:

我正在使用 webdriver 试用 phantomJS,但在处理 javascript 警报时遇到了问题。我注意到 phantomjs 驱动程序desired_capabilities 有一个字段'handlesAlerts': False 有没有办法将此值设置为true?我已经尝试了明显的方法,但没有任何效果:

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 中的值,但是当我尝试切换到警报时,我收到一条错误消息。

$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"}' 

【问题讨论】:

    标签: python selenium phantomjs


    【解决方案1】:

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

    这就是这种情况下实际发生的情况。 PhantomJS 驱动程序不支持处理警报,如the source code 中所示,返回的功能对象表明了这一点。在大多数语言绑定中,此返回的功能对象是只读的;在返回对象可能是读写的语言绑定中,修改这些功能对会话没有实际影响。在待处理的W3C WebDriver specification 中,有一个requiredCapabilities 设置,如果服务器无法提供该功能,则会引发异常,但据我所知,尚未由任何驱动程序实现。

    【讨论】:

    • 维基页面具有误导性。 API 实际上是这样实现的,以便您传递所需的功能,并且远程端使用它能够创建的内容创建会话。远程端返回会话中实际可用的功能,并且该通信是单向的(本地编辑不会影响远程会话)。在待处理的W3C WebDriver specification 中,有一个requiredCapabilities 设置,如果服务器无法提供该功能,则会引发异常,但据我所知,尚未由任何驱动程序实现。
    • 投反对票的人,即使你不喜欢真相,我也认为投反对票没有意义
    • @usmcs 很抱歉,我的幽默尝试妨碍了我,看起来像“snark”。那不是本意。我已经重写了答案以更好地表达驱动程序中的情况。另请注意,有问题的 wiki 页面已更新,以便更清楚地了解这种情况。其他反对者,这个答案描述了事情的方式。仅仅因为这不是您想听到的,或者不是您想要的方式,并不会降低答案的有效性或正确性。
    猜你喜欢
    • 1970-01-01
    • 2013-01-10
    • 1970-01-01
    • 2019-06-29
    • 2019-09-22
    • 2022-11-29
    • 2021-07-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多