20

I am scraping a webpage using Selenium webdriver in Python

The webpage I am working on, has a form. I am able to fill the form and then I click on the Submit button.

It generates an popup window( Javascript Alert). I am not sure, how to click the popup through webdriver.

Any idea how to do it ?

Thanks

Kiran
  • 8,034
  • 36
  • 110
  • 176
  • See http://code.google.com/p/selenium/wiki/FrequentlyAskedQuestions#Q:_Does_WebDriver_support_Javascript_alerts_and_prompts? It's not Python, but I think it is quite understandable. – rubik Dec 25 '11 at 20:54
  • Ya. But it does not work with Python. I have not been able to find an equivalent function to handle popup window. – Kiran Dec 25 '11 at 21:18
  • possible duplicate of [How to click and verify the existence of a pop up (alert)](http://stackoverflow.com/questions/3084850/how-to-click-and-verify-the-existence-of-a-pop-up-alert) – Acorn Dec 25 '11 at 23:53
  • 2
    Well its not. My question was related to webdriver, and the question you are referring to is concerning selenium. – Kiran Dec 26 '11 at 15:12

6 Answers6

27

Python Webdriver Script:

from selenium import webdriver

browser = webdriver.Firefox()
browser.get("http://sandbox.dev/alert.html")
alert = browser.switch_to_alert()
alert.accept()
browser.close()

Webpage (alert.html):

<html><body>
    <script>alert("hey");</script>
</body></html>

Running the webdriver script will open the HTML page that shows an alert. Webdriver immediately switches to the alert and accepts it. Webdriver then closes the browser and ends.

If you are not sure there will be an alert then you need to catch the error with something like this.

from selenium import webdriver

browser = webdriver.Firefox()
browser.get("http://sandbox.dev/no-alert.html")

try:
    alert = browser.switch_to_alert()
    alert.accept()
except:
    print "no alert to accept"
browser.close()

If you need to check the text of the alert, you can get the text of the alert by accessing the text attribute of the alert object:

from selenium import webdriver

browser = webdriver.Firefox()
browser.get("http://sandbox.dev/alert.html")

try:
    alert = browser.switch_to_alert()
    print alert.text
    alert.accept()
except:
    print "no alert to accept"
browser.close()
Mike Grace
  • 16,636
  • 8
  • 59
  • 79
  • 4
    switch_to_alert() is now deprecated in the latest Selenium Python bindings v 2.46.0. Use driver.switch_to.alert instead. Source: http://selenium-python.readthedocs.org/en/latest/api.html – SpartaSixZero Jul 01 '15 at 20:55
4
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Firefox()
#do something
if EC.alert_is_present:
    print "Alert Exists"
    driver.switch_to_alert().accept()
    print "Alert accepted"
else:
    print "No alert exists"

More about excepted_conditions https://seleniumhq.github.io/selenium/docs/api/py/webdriver_support/selenium.webdriver.support.expected_conditions.html

Greg Sadetsky
  • 4,863
  • 1
  • 38
  • 48
  • 1
    @bad_deadpool thanks for the update! also: that link is 404 -- that doc is now located at http://selenium-python.readthedocs.io/api.html#selenium.webdriver.remote.webdriver.WebDriver.switch_to_alert – Greg Sadetsky May 08 '17 at 22:36
2

If you want to Accept or Click the popup, regardless of for what it is then

alert.accept

Where alert is object of class selenium.webdriver.common.alert.Alert(driver) and accept is method of that object

Source

Gaurav Gandhi
  • 3,041
  • 2
  • 27
  • 40
1

Try The Code below! Working Fine for me!

alert = driver.switch_to.alert
try:
   alert.accept() #If you want to Accept the Alert
except:
   alert.dismiss()  #If  You want to Dismiss the Alert.
Amar Kumar
  • 2,392
  • 2
  • 25
  • 33
1

I am using Ruby bindings but here what I found in Selenium Python Bindings 2 documentation: http://readthedocs.org/docs/selenium-python/en/latest/index.html

Selenium WebDriver has built-in support for handling popup dialog boxes. After you’ve triggerd and action that would open a popup, you can access the alert with the following:

alert = driver.switch_to_alert()

Now I guess you can do something like that:

if alert.text == 'A value you are looking for'
  alert.dismiss
else
  alert.accept
end

Hope it helps!

Yulia
  • 1,575
  • 4
  • 18
  • 27
0

that depends on the javascript function that handles the form submission if there's no such function try to submit the form using post

simonzack
  • 19,729
  • 13
  • 73
  • 118
  • I get a simple javascript alert saying "Thanks for submission", I would like to just press an enter to close the popup window. – Kiran Dec 25 '11 at 21:19