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,320 points
31,632 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()

For further understanding, you can refer to the Selenium Course.

answered Jun 20, 2018 by Samarpit
• 5,910 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, 2019 in Selenium by Abha
• 28,140 points
5,442 views
0 votes
1 answer

Cannot click on a button using selenium

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

answered Jan 4, 2019 in Selenium by Dushyant
3,647 views
0 votes
2 answers

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,500 points
13,673 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
9,142 views
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,910 points
7,221 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 3, 2020 in Selenium by Sri
• 3,190 points
7,874 views
+1 vote
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,910 points
5,321 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,910 points
7,275 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP