Selenium Desired Capabilities - set handlesAlerts

2019-01-25 07:55发布

问题:

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,...}

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

回答1:

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.

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.