简体   繁体   中英

How to close browser pop-ups using Selenium in python?

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)

You should first try this in order to see if it's an error related to waits.
ie : if your browser doesn't have enough time to find the alert dialog before making actions. You can learn more about explicit waits. here

you will also need expected_conditions package and try the following:

# add these imports
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException

#try to find the alert and do stuff
try:
    #wait for the alert to show up
    WebDriverWait(browser, 3).until(EC.alert_is_present(),
                               'Timed out waiting for PA creation ' +
                               'confirmation popup to appear.')
    #if it does
    alert = browser.switch_to.alert()
    alert.accept()
    print "alert accepted"
except TimeoutException:
    print "no alert"

Note that i precised to wait for 3 seconds, you can change it to whatever you like.

if this solution doesn't work, you should try a trick to explicitly select the dialog popup by name or text etc..

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM