dnisqa2 delta
dnisqa2 delta

Reputation: 161

How to click on a button within a pop-up window with python selenium?

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.

Upvotes: 5

Views: 10123

Answers (2)

undetected Selenium
undetected Selenium

Reputation: 193088

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()

Upvotes: 5

Akash Chavan
Akash Chavan

Reputation: 52

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.

Upvotes: 2

Related Questions