Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I click a particular element in a pop up?

I have written the following selenium script:

from selenium import webdriver
PATH= r"C:\Users\David\Desktop\Selenium\chromedriver.exe"
driver=webdriver.Chrome(PATH)

driver.get("https://www.studentbeans.com/uk")

When I enter the website, there is a pop up that appears that asks if I’d like to accept all cookies. I would like to click yes. How do I add this onto my code?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Firstly, you have to select the cookie element using xpath or css selector then click by calling click() function.Remember it that you also need to make full screen using driver.maximize_window()

Try:

import time
from selenium import webdriver
PATH= r"C:\Users\David\Desktop\Selenium\chromedriver.exe"
driver=webdriver.Chrome(PATH)

driver.get("https://www.studentbeans.com/uk")
driver.maximize_window()
time.sleep(4)
cookie_button=driver.find_element_by_xpath('//button[@id="onetrust-accept-btn-handler"]').click()
time.sleep(2)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading