我尝试用webdriver的phantomJS和我在与处理JavaScript警报麻烦。 我注意到phantomjs司机desired_capabilities有一个字段'handlesAlerts': False
有没有一种方法来设置这个值是真的吗? 我已经尝试了明显的方法,但不具有任何影响:
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"}'