Narendra Modi
Narendra Modi

Reputation: 861

Handling popup windows in Selenium

I am entering a password and checking if it's work or not. enter image description here

After entering wrong password, site reloads and pop up appears. enter image description here

How do I deal with the popup? For example, how can I automatically click an element in the popup?

My Code:

from selenium.webdriver.support import expected_conditions as EC
     while (2>1):
              Sam = browser.find_element_by_css_selector("input[id=1]")
              Sam.send_keys(i)
              login = browser.find_element_by_css_selector("input[id=2]")
              login.click()
              if EC.alert_is_present:
                   browser.switch_to.alert.accept()
              else:
                  print i
                  break;

I am getting this error:

Traceback (most recent call last):
  File "<pyshell#58>", line 1, in <module>
    sexy()
  File "<pyshell#57>", line 3, in sexy
    browser.get('http://eps.gpeonline.co.in/')
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 264, in get
    self.execute(Command.GET, {'url': url})
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 252, in execute
    self.error_handler.check_response(response)
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response
    raise exception_class(message, screen, stacktrace)
WebDriverException: Message: Failed to decode response from marionette

Upvotes: 1

Views: 1494

Answers (1)

Andersson
Andersson

Reputation: 52665

You use EC.alert_is_present incorrectly: your if condition will always return True as EC.alert_is_present is just a class. Try to use below try/except block instead of if/else:

from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait as wait

try:
    wait(browser, 1).until(EC.alert_is_present()).accept()
except TimeoutException:
    print i
    break

This should allow you to accept alert if it present or print i and break loop if it didn't appear in 1 second (you can change timeout value if required)

Upvotes: 1

Related Questions