5

I have a question about click button on a pop-up window. The GUI as below: GUI

HTML content as below: HTML

I'm trying to use python selenium to click the "OK" button in many ways: For example:

driver.switch_to_alert()
driver.find_element_by_id("YesBtn").click()

or

driver.switch_to_alert()
driver.find_element_by_xpath("//div[@id='YesBtn']").click()

or

driver.switch_to_alert()
driver.find_element_by_xpath("//input[@id='YesBtn']/html/body/div/div/div/div/div[3]").click()

But I always get error message like:

Unable to locate element: {"method":"id","selector":"YesBtn"}

Is there anyone can help me to correct the code? Many thanks.

1
  • 2
    You should not use driver.switch_to_alert() as it's not an alert at all. Note that driver.switch_to_alert() might be applied to object triggered by JavaScript alert() function and it doesn't have HTML source code. Check whether alertpopupDiv located inside an iframe Commented Mar 26, 2018 at 8:37

2 Answers 2

5

As per the HTML you have shared it's not an Alert but a Modal Dialog Box. To click on the element with text as OK you have to induce WebDriverWait in-conjunction with expected_conditions clause set to element_to_be_clickable as follows :

WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='btn btn-primary' and @id='YesBtn']"))).click()
Sign up to request clarification or add additional context in comments.

Comments

2
alert = driver.switch_to_alert()
alert.accept()

This will return the currently open alert object. With this object, you can now accept, dismiss, read its contents or even type into a prompt.

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.