Handling "Accept Cookies" popup with Selenium in Python Handling "Accept Cookies" popup with Selenium in Python selenium selenium

Handling "Accept Cookies" popup with Selenium in Python


You were very close!

If you open your page in a new browser you'll note the page fully loads, then, a moment later your popup appears.

The default wait strategy in selenium is just that the page is loaded. That draw delay between page loaded and display appearing is causing your scripts to fail.

You have two good synchronisation options.

1/Include an implicit wait for your driver. This is done once per script and affects all objects. This waits 10 seconds before throwing any error, or continues when it's ready:

PATH = "/usr/bin/chromedriver"driver = webdriver.Chrome(PATH)driver.implicitly_wait(10)driver.get("https://www.immoweb.be/en/search/house/for-sale?countries=BE&page=1&orderBy=relevance")driver.find_element_by_xpath('//*[@id="uc-btn-accept-banner"]').click()

2/Do a explicit wait on your object only:

PATH = "/usr/bin/chromedriver"driver = webdriver.Chrome(PATH)driver.get("https://www.immoweb.be/en/search/house/for-sale?countries=BE&page=1&orderBy=relevance")WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,'//*[@id="uc-btn-accept-banner"]'))).click()

More info on the wait strategies is here