Problem: Using selenium+phantomjs to simulate login, the web page pops up a box with alert(“login successful”) in JavaScript, but with switch_to_alert().accept() it reports an error and cannot execute the command.
Target code: <script language=”javascript”>alert(‘********************’);</script>
Error displayed:
File “sy.py”, line 112, in <module>
Pop123= Pop_up.text
File “C:\Python27\lib\site-packages\selenium\webdriver\common\alert.py”, line 69, in text
return self.driver.execute(Command.GET_ALERT_TEXT)[“value”]
File “C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py”, line 308, in execute
self.error_handler.check_response(response)
File “C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py”, line 165, in check_response
raise exception_class(value)
selenium.common.exceptions.WebDriverException: Message: Invalid Command Method – {“headers”:{“Accept”:”application/json”,”Accept-Encoding”:”identity”,”Connection”:”close”,”Content-Type”:”application/json;charset=UTF-8″,”Host”:”127.0.0.1:59198″,”User-Agent”:”Python http auth”},”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/95d315d0-ce94-11e7-8d72-c1b9396af0ea/alert_text”}
Other Solutions:
phantomjs is no interface, can not j parse dynamic JS, so for alert is also unable to pop up directly, so phantomjs to the function callback in page in the execution of the alert event.
python does not correspond to the phentomjs special library, so you need to call JS.
python selenium officially no longer provides support for phentomjs. Now the main Firefox browser and Google Chrome browser, please try to change
I. python call Js
r = driver.execute_script(“return newsJason”)
print r
Second, Js inside the method time is busy not to test the right and wrong
page.onAlert = function(msg) {}
driver.execute_script(“window.confirm = function(msg) { return true; }”);