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"}'
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…