无法找到“接受”按钮 - 硒 - 初学者网络刮擦

发布于 2025-02-06 12:12:42 字数 912 浏览 2 评论 0原文

我正在尝试使用硒来学习不同的网络刮擦方式。

当执行代码时,Firefox启动并“接受cookie”或弹出的内容。在检查页面时,我无法找到“接受”按钮。

到目前为止,我的代码:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
import pandas as pd
import time

PATH = "C:/Users/myuser/Desktop/Driver/geckodriver.exe"

driver = webdriver.Firefox(executable_path=PATH)


driver.maximize_window() # For maximizing window
driver.get("https://www.immonet.de/")


button_pos = driver.find_element(by=By.CLASS_NAME, value="sc-gsDKAQ fILFKg")
button_pos.click()
print(driver.title)



input = input()

我会收到以下错误:无法找到元素:.sc-scdkaq filfkg

我的想法是通过Inspect工具找到按钮:

”我想念还是做错了?我如何找到合适的元素?

谢谢! 拍

I am trying to use Selenium in order to learn different ways of web scraping.

When the code is executed Firefox starts and the "accept cookies" or what ever pops up. I am unable to locate the "accept" button when inspecting the page.

my code so far:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
import pandas as pd
import time

PATH = "C:/Users/myuser/Desktop/Driver/geckodriver.exe"

driver = webdriver.Firefox(executable_path=PATH)


driver.maximize_window() # For maximizing window
driver.get("https://www.immonet.de/")


button_pos = driver.find_element(by=By.CLASS_NAME, value="sc-gsDKAQ fILFKg")
button_pos.click()
print(driver.title)



input = input()

I get the following error: Unable to locate element: .sc-gsDKAQ fILFKg

My thought was locating the button via the inspect tool as follows:

enter image description here

What am I missing or doing wrong? How would i find the right element?

Thanks!
Pat

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清旖 2025-02-13 12:12:42

首先,要显示此URL,接受cookie是必须的,但是要接受并单击cookie按钮并不是一件容易的任务,因为cookie按钮在阴影根(打开)selenium和webdriverwait在阴影root上无能为力,因此,要执行阴影根,您需要应用JavaScript QuerySelector。

#执行阴影根并接受cookie

driver.execute_script('''return document.querySelector('div#usercentrics-root').shadowRoot.querySelector('button[data-testid="uc-accept-all-button"]')''').click()

First of all, to display this url,accepting the cookies is a must but to accept and click on the cookie button isn't a easy task because cookies button is under shadow root (open) selenium and webdriverWait can do nothing on shadow root,so to execute shadow root you need to apply JavaScript querySelector.

#To execute shadow root and accept cookies

driver.execute_script('''return document.querySelector('div#usercentrics-root').shadowRoot.querySelector('button[data-testid="uc-accept-all-button"]')''').click()
梦里人 2025-02-13 12:12:42

HTML元素中的类属性可以包含由空间分开的多个类。即“ sc-gsdkaq filfkg”,包含两个类,sc-gsdkaqfilfkg

您可以用户使用,但两者都是随机的,并且可以在下次重新编译CSS时进行更改。我建议使用data-Testid属性来考虑XPath

Class attribute in the html element can contain multiple classes separated by space. i.e. "sc-gsDKAQ fILFKg", contains two classes, sc-gsDKAQ and fILFKg.

You can user either but both are random and can be changed next time css is recompiled. I recommend to think of xpath using data-testid attribute

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文