Home > OS >  Python: Floating cookie element can't be inspected with Chrome's dev tools and keyboard ac
Python: Floating cookie element can't be inspected with Chrome's dev tools and keyboard ac

Time:07-16

We have quite nice coding program in our school and we did web scraping project with Python before holidays. Of course I had to take this a bit further and I am still working on this.

My problem is that I have no idea how to handle cookies on this site with Python. https://www.s-kaupat.fi/

I've tried to inspecting this element but I can't find this element with developer tools with Chrome or Firefox. I also tried pyautoqui and keyboard but they are just pressing keys behind the floating cookie element.

I'm also accepting all cookies with Chrome but it seems not work.

So, my question is how do I accept cookies or how could I see what is this element and find its xpath?

CodePudding user response:

Your problem appears to be that the cookie popup resides inside a shadow-root. At some point I solved a similar problem something like this:

shadow_root = driver.execute_javascript("return arguments[0].shadowRoot", element)

desired_element_on_popup = shadow_root.find_element(By.CSS_SELECTOR, 'selector for whatever element you want')

CodePudding user response:

This is what I have so far...

from selenium import webdriver
from selenium.webdriver.remote.webelement import WebElement
from selenium.webdriver.common.by import By
import time

options = webdriver.ChromeOptions
options.add_argument('javascipt.enabled', True)

driver = webdriver.Chrome('.../seleniumbase/drivers/chromedriver', options=options)
url = ('https://www.s-kaupat.fi')

driver.get(url)
time.sleep(3)

sh = driver.find_element((By.css(CSS_SHADOW_HOST)))
sr = driver.execute_script("return arguments[0],shadowRoot",sh)

print(sr)

This gives me error that I don't understand at all. I just want to get rid of that popup that asks cookie permission.

With keyboard I can press 6 times TAB key and then press ENTER to get rid of this.

  • Related