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

Viewed 36

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?

2 Answers

Inspect the HTML, and find that element. Then copy the xpath or css selector or whatever and:

driver.find_element_by_xpath('copy xpath').click()

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