Click on a button within a pop-up window with python selenium

0 votes

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"}

Please help in this matter

Jun 20, 2018 in Selenium by Martin
• 4,220 points
8,006 views

1 answer to this question.

0 votes

It's not an Alert but a Modal Dialog Box. 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()

answered Jun 20, 2018 by Samarpit
• 5,130 points
What does 'EC' refer to in this answer?

EC stands for Expected Conditions. In some websites, the elements load dynamically. You have to wait for these web elements to load to use them. In such cases, you use Expected Conditions to specify the conditions. 

Thanks Rishi, for clarifying the keyword. You explained it really well.

Related Questions In Selenium

0 votes
2 answers

Python & Selenium: How to find and click on YouTube's Like button

Hello Nitin, as the Like button on ...READ MORE

answered Aug 23 in Selenium by Abha
• 27,180 points
703 views
0 votes
1 answer

Cannot click on a button using selenium

The error that I figured out was ...READ MORE

answered Jan 4 in Selenium by Dushyant
209 views
0 votes
1 answer

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,110 points
943 views
0 votes
1 answer
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,130 points
1,121 views
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,130 points
859 views
0 votes
1 answer

Angular JS: Selenium cannot click a button by python code on mac

You can try clicking it with javascript ...READ MORE

answered Aug 9, 2018 in Selenium by Samarpit
• 5,130 points
936 views
0 votes
1 answer

How to click a span with given text using Selenium Python

If ALL_USA is subject to change, then ...READ MORE

answered Jul 26, 2018 in Selenium by Samarpit
• 5,130 points
2,057 views