1

The program using Selenium

from selenium import webdriver
browser = webdriver.Firefox()
url = 'any_url'
browser.get(url)
browser.find_element_by_id('empty_cart_btn').click()

creates the following dialog box:

enter image description here

What is the code in the Selenium to automatically Press OK?

1 Answer 1

4

Try accept() from selenium.webdriver.common.alert

Untested code below:

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

browser = webdriver.Firefox()
url = 'any_url'
browser.get(url)
browser.find_element_by_id('empty_cart_btn').click()

try:
    WebDriverWait(browser, 5).until(EC.alert_is_present(), 'Waiting for alert timed out')

    alert = browser.switch_to_alert()
    alert.accept()
    print "alert accepted"

except TimeoutException:
    print "no alert"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.