I'm trying to automate a download using Python and Selenium. In the start page, a pop-up appears on the page:
How can I close it using Selenium?
I tried the following ways but all failed:
>>> alert = browser.switch_to_alert()
>>> alert.accept()
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/usr/lib/python2.7/dist-packages/selenium/webdriver/common/alert.py", line 80, in accept
self.driver.execute(Command.ACCEPT_ALERT)
File "/usr/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 201, in execute
self.error_handler.check_response(response)
File "/usr/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 181, in check_response
raise exception_class(message, screen, stacktrace)
NoAlertPresentException: Message: no alert open
(Session info: chrome=59.0.3071.115)
(Driver info: chromedriver=2.30.477691 (6ee44a7247c639c0703f291d320bdf05c1531b57),platform=Linux 4.4.0-57-generic x86_64)
>>> alert.dismiss()
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/usr/lib/python2.7/dist-packages/selenium/webdriver/common/alert.py", line 71, in dismiss
self.driver.execute(Command.DISMISS_ALERT)
File "/usr/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 201, in execute
self.error_handler.check_response(response)
File "/usr/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 181, in check_response
raise exception_class(message, screen, stacktrace)
NoAlertPresentException: Message: no alert open
(Session info: chrome=59.0.3071.115)
(Driver info: chromedriver=2.30.477691 (6ee44a7247c639c0703f291d320bdf05c1531b57),platform=Linux 4.4.0-57-generic x86_64)
>>>alert = browser.switch_to_window('Open xdg-open?')
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/usr/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 531, in switch_to_window
self._switch_to.window(window_name)
File "/usr/lib/python2.7/dist-packages/selenium/webdriver/remote/switch_to.py", line 92, in window
self._driver.execute(Command.SWITCH_TO_WINDOW, data)
File "/usr/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 201, in execute
self.error_handler.check_response(response)
File "/usr/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 181, in check_response
raise exception_class(message, screen, stacktrace)
NoSuchWindowException: Message: no such window
(Session info: chrome=59.0.3071.115)
(Driver info: chromedriver=2.30.477691 (6ee44a7247c639c0703f291d320bdf05c1531b57),platform=Linux 4.4.0-57-generic x86_64)
>>> alert = browser.switch_to.window("Open xdg-open?")
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/usr/lib/python2.7/dist-packages/selenium/webdriver/remote/switch_to.py", line 92, in window
self._driver.execute(Command.SWITCH_TO_WINDOW, data)
File "/usr/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 201, in execute
self.error_handler.check_response(response)
File "/usr/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 181, in check_response
raise exception_class(message, screen, stacktrace)
NoSuchWindowException: Message: no such window
(Session info: chrome=59.0.3071.115)
(Driver info: chromedriver=2.30.477691 (6ee44a7247c639c0703f291d320bdf05c1531b57),platform=Linux 4.4.0-57-generic x86_64)
The problem you face is, that the popup is not part of the DOM and thus can neither be handled by selenium by sending keystrokes nor waiting nor clicking somewhere. The popup is a browser native popup and thus can only be handled by the browser itself, usually by user interaction.
To prevent the interaction you can define beforehand which action the browser should take in the circumstance that a link uses a certain protocol. Think hrefs with telephone links:
<p>Book now, call <a href="tel:01234567890">01234 567 890</a></p>
The user preferences have to be changed on/before browser start. The handling of protocol schemes can be predefined in the user preferences. In my case I wanted to deny handling of the scheme tel://
.
To change the user preferences on startup extend the browsers capabilities and specify user preferences under prefs
of the chromeOptions
:
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_experimental_option( "prefs", {'protocol_handler.excluded_schemes.tel': false})
driver = webdriver.Chrome(chrome_options=chrome_options)
In your case it is probably a googlePlay link. So instead of tel: false
use the protocol in question instead of tel
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With