无法找到“接受”按钮 - 硒 - 初学者网络刮擦
我正在尝试使用硒来学习不同的网络刮擦方式。
当执行代码时,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:
What am I missing or doing wrong? How would i find the right element?
Thanks!
Pat
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,要显示此URL,接受cookie是必须的,但是要接受并单击cookie按钮并不是一件容易的任务,因为cookie按钮在阴影根(打开)selenium和webdriverwait在阴影root上无能为力,因此,要执行阴影根,您需要应用JavaScript QuerySelector。
#执行阴影根并接受cookie
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
HTML元素中的类属性可以包含由空间分开的多个类。即
“ sc-gsdkaq filfkg”
,包含两个类,sc-gsdkaq
和filfkg
。您可以用户使用,但两者都是随机的,并且可以在下次重新编译CSS时进行更改。我建议使用
data-Testid
属性来考虑XPathClass attribute in the html element can contain multiple classes separated by space. i.e.
"sc-gsDKAQ fILFKg"
, contains two classes,sc-gsDKAQ
andfILFKg
.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