草庐IT

python - Selenium Desired Capabilities - 为 PhantomJS 驱动程序设置 handlesAlerts

coder 2023-08-15 原文

我正在使用 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"}' 

最佳答案

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

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

关于python - Selenium Desired Capabilities - 为 PhantomJS 驱动程序设置 handlesAlerts,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16022226/

有关python - Selenium Desired Capabilities - 为 PhantomJS 驱动程序设置 handlesAlerts的更多相关文章

随机推荐