Selenium Desired Capabilities - set handlesAlerts for PhantomJS driver


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',
'driverName': u'ghostdriver',
'driverVersion': u'1.0.3',
'handlesAlerts': False,
'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 
<script type="text/javascript">
Hello World.
>>> 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):
.common.exceptions.WebDriverException: Message:
'Invalid Command Method - Request =>
"Accept- Encoding":"identity",
"Content-Type":"application/json;charset=UTF- 8",
"User-Agent":"Python- urllib/2.7"},
"urlParsed": {"anchor":"",
"relative":"/ alert_text",
"protocol ":"",
  • You must to post comments

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.

  • You must to post comments
Showing 1 result
Your Answer
Post as a guest by filling out the fields below or if you already have an account.