[英]Python: Floating cookie element can't be inspected with Chrome's dev tools and keyboard actions are working behind this element
We have quite nice coding program in our school and we did web scraping project with Python before holidays.我们学校有相当不错的编码程序,我们在假期前用 Python 做了网页抓取项目。 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.我的问题是我不知道如何用 Python 处理这个站点上的 cookie。 https://www.s-kaupat.fi/ 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.我已尝试检查此元素,但使用 Chrome 或 Firefox 的开发人员工具找不到此元素。 I also tried pyautoqui and keyboard but they are just pressing keys behind the floating cookie element.我还尝试了 pyautoqui 和键盘,但它们只是在浮动 cookie 元素后面按下键。
I'm also accepting all cookies with Chrome but it seems not work.我也接受 Chrome 的所有 cookie,但它似乎不起作用。
So, my question is how do I accept cookies or how could I see what is this element and find its xpath?所以,我的问题是我如何接受 cookie,或者我如何才能看到这个元素是什么并找到它的 xpath?
Your problem appears to be that the cookie popup resides inside a shadow-root
.您的问题似乎是 cookie 弹出窗口位于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')
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.我只是想摆脱那个要求 cookie 许可的弹出窗口。
With keyboard I can press 6 times TAB key and then press ENTER to get rid of this.用键盘我可以按 6 次 TAB 键,然后按 ENTER 来摆脱这个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.