[英]Javascript alerts in Python + Selenium + PhantomJS script
I try to "click" Javascript alert for reboot confirmation in DSL modem with a Python script as follows: 我尝试使用Python脚本“点击”用于DSL调制解调器中的重新启动确认的Javascript警报,如下所示:
#!/usr/bin/env python
import selenium
import time
from selenium import webdriver
cap = {u'acceptSslCerts': True,
u'applicationCacheEnabled': True,
u'browserConnectionEnabled': True,
u'browserName': u'phantomjs',
u'cssSelectorsEnabled': True,
u'databaseEnabled': False,
u'driverName': u'ghostdriver',
u'driverVersion': u'1.1.0',
u'handlesAlerts': True,
u'javascriptEnabled': True,
u'locationContextEnabled': False,
u'nativeEvents': True,
u'platform': u'linux-unknown-64bit',
u'proxy': {u'proxyType': u'direct'},
u'rotatable': False,
u'takesScreenshot': True,
u'version': u'1.9.8',
u'webStorageEnabled': False}
driver = webdriver.PhantomJS('/usr/lib/node_modules/phantomjs/bin/phantomjs', desired_capabilities=cap)
driver.get('http://username:passwd@192.168.1.254')
sbtn = driver.find_element_by_id('reboto_btn')
sbtn.click()
time.sleep(4)
al = driver.switch_to_alert()
print al.accept()
However, I get exception pasted below even though I do set handlesAlerts
in desired_capabilities
. 但是,即使我在
desired_capabilities
设置handlesAlerts
,我handlesAlerts
在下面粘贴异常。
How can I fix that? 我该如何解决这个问题? What's the reason for the exception?
异常的原因是什么?
Exception: 例外:
---------------------------------------------------------------------------
WebDriverException Traceback (most recent call last)
/usr/local/bin/pjs/asus_reboot.py in <module>()
36 #ipdb.set_trace()
37
---> 38 print al.accept()
39
40 #print al.text
/usr/local/venvs/asusreboot/local/lib/python2.7/site-packages/selenium/webdriver/common/alert.pyc in accept(self)
76 Alert(driver).accept() # Confirm a alert dialog.
77 """
---> 78 self.driver.execute(Command.ACCEPT_ALERT)
79
80 def send_keys(self, keysToSend):
/usr/local/venvs/asusreboot/local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.pyc in execute(self, driver_command, params)
173 response = self.command_executor.execute(driver_command, params)
174 if response:
--> 175 self.error_handler.check_response(response)
176 response['value'] = self._unwrap_value(
177 response.get('value', None))
/usr/local/venvs/asusreboot/local/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.pyc in check_response(self, response)
134 if exception_class == ErrorInResponseException:
135 raise exception_class(response, value)
--> 136 raise exception_class(value)
137 message = ''
138 if 'message' in value:
WebDriverException: Message: Invalid Command Method - {"headers":{"Accept":"application/json","Accept-Encoding":"identity","Connection":"close","Content-Length":"53","Content-Type":"application/json;charset=UTF-8","Host":"127.0.0.1:36590","User-Agent":"Python-urllib/2.7"},"httpVersion":"1.1","method":"POST","post":"{\"sessionId\": \"fc97c240-f098-11e4-ae53-e17f38effd6c\"}","url":"/accept_alert","urlParsed":{"anchor":"","query":"","file":"accept_alert","directory":"/","path":"/accept_alert","relative":"/accept_alert","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/accept_alert","queryKey":{},"chunks":["accept_alert"]},"urlOriginal":"/session/fc97cea0-f098-11e4-ae53-e17f38eaad6c/accept_alert"}
由于PhantomJs不支持Alert框。你需要使用执行程序。
driver.execute_script("window.confirm = function(msg) { return true; }");
In java, driver.switchTo().alert().accept(); 在java中,driver.switchTo()。alert()。accept(); will do the job.
会做的。
I am not sure, why you are using "print al.accept()", probably are you trying to print text? 我不确定,为什么你使用“print al.accept()”,你可能正在尝试打印文本? then alert.getText() will do in java, sorry if i am wrong, because i am sure in python.
然后alert.getText()将在java中做,抱歉,如果我错了,因为我确信在python中。
Thank You, Murali http://seleniumtrainer.com/ 谢谢你,Murali http://seleniumtrainer.com/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.