简体   繁体   English

Python:无法使用 Chrome 的开发工具检查浮动 cookie 元素,并且键盘操作正在此元素后面工作

[英]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.

相关问题 使用 Python 时无法在 chrome 开发工具中提取正确的元素 - Cannot extract correct element in chrome dev tools when using Python Selenium - 在检查 HTML 之前找不到可见元素? - Selenium - can't find visible element until HTML is inspected? 有没有办法通过 Python 加载网页的网络活动(您可以在 Chrome Dev Tools 上看到)? - Is there a way to get a webpage's Network activity (which you can see on Chrome Dev Tools) on load via Python? Python 找不到那里的元素 - Python can't find element that's there Selenium Python:Actions.move_to_element 不工作 - Selenium Python: Actions.move_to_element not working 为什么从带有 javascript 的模板中看不到乘法的结果,但在 chrome 开发工具中检查元素时却看到了? - Why is the result of the multiplication not seen from the template with javascript but when inspecting the element in chrome dev tools it is? beautifulsoup 通过开发工具“复制选择器”查找元素 - beautifulsoup find element by dev tools “copy selector” Selenium无法通过其ID访问网络元素(Python) - Selenium can't access web-element by it's id (python) 无法在谷歌浏览器网页上找到元素 - Can't locate element on google chrome webpage Python找不到这样的元素 - Python can't find No such element
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM