I’m trying to close a cookie consent pop-up on a site https://www.meinschiff.com/. I used selenium webdriver for chrome to find the close and accept div element and click on whichever is found first. If I manually choose deny button, then it is closed but it hangs with a loading of another layer. The element’s parent’s parent div element with id="blur" acts as an ever loading overlay. The code fetches the accept button first and clicks on it but it refreshes the page giving StaleElementReference error. The clicks work perfectly when I open the site in a normal chrome browser.
I want to click accept button and close the pop up.
My code:
div_elements = WebDriverWait(driver,
5).until(EC.presence_of_all_elements_located((By.TAG_NAME, "div")))
for div_element in div_elements:
try:
element_html = div_element.get_attribute("outerHTML")
div_element_attrs = BeautifulSoup(element_html, "lxml").div.attrs
# class_attr = div_element.get_attribute('class')
# title_attr = div_element.get_attribute('title')
value_attr = div_element.get_attribute('value')
popup_div = False
for key in div_element_attrs.keys():
if any(keyword in (div_element.get_attribute(key) or '').lower()
for keyword in close_keywords) or
(value_attr is not None and value_attr.lower() == 'false'):
print("closing div pop-up...")
popup_div = True
break
else:
if any(keyword in (div_element.get_attribute(key) or
'').lower() for keyword in accept_keywords) or
(value_attr is not None and value_attr.lower() == 'false'):
print("accepting div pop-up...")
popup_div = True
break
if popup_div:
ActionChains(driver).move_to_element(div_element).click().perform()
time.sleep(2)
if div_element.is_displayed():
print("div element not closed")
return False
else:
print("div element closed")
return True
except ElementClickInterceptedException:
print(f"Div Element click intercepted:
{div_element.get_attribute('outerHTML')}")
continue
except Exception as e:
print(f"Error processing div element: {e}")
continue
I have also tried javascript click with:
driver.execute_script("arguments[0].scrollIntoView(true);", div_element)
driver.execute_script("arguments[0].click();", div_element)
and wait for element to be clickable and it results in same behavior:
clickable_element = WebDriverWait(driver,
10).until(EC.element_to_be_clickable(div_element))
ActionChains(driver).move_to_element(clickable_element).click().perform()
2
Answers
Solution: I have set allow cookies to True in the selenium chrome options prefs settings and now the button is clickable and closes the popup. I'm still getting StaleElementReferenceError because the page gets refreshed and found element is erased from the driver.
I don’t know if i have truly understand your post but like @browsermator say’s, it seem’s to be a timing issue.
You can use Implicit Waits : Selenium-python Documentation
The script below launch the driver, set an implicit waits of 10 seconds, go to meinschiff webpage , deny cookie and print the slogan of meinschiff :
I hope it will help you. Feel free to tell us if your issu is solved.