点击cookie按钮但结果不一致

问题描述 投票:0回答:1

我目前正在尝试抓取以下页面

我在单击 cookie 按钮时遇到问题,因为它运行不一致。在某些运行中,单击按钮并且该过程按应有的方式继续,但在其他情况下,它只是停止并且出现以下错误:

[24812:22288:0512/162346.912:ERROR:chrome_browser_cloud_management_controller.cc(161)] Cloud management controller initialization aborted as CBCM is not enabled. Please use the `--enable-chrome-browser-cloud-management` command line flag to enable it if you are not using the official Google Chrome build.

“[24812:22668:0512/162346.939:ERROR:sandbox_win.cc(895)] Sandbox cannot access executable. Check  filesystem permissions are valid. See https://bit. ly/31yqMJR.: Access is denied. (0x5)”

“DevTools listening on ws://127.0.0.1:65082/devtools/browser/d3c31f38-a536-4c0c-96d6-89734ca592e6[24812:22288:0512/162347.064:ERROR:network_service_instance_impl.cc(599)] Network service crashed, restarting service.”

这是我正在使用的代码(它更长,但有时它停止,它不会运行超过这一点):

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from bs4 import BeautifulSoup
import requests
import re

chrome_options = Options()

chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')

driver = webdriver.Chrome(options=chrome_options)

try:
    
    url = 'https://www.arbeitsagentur.de/jobsuche/suche?angebotsart=34&was=Programmierer%2Fin&sort=veroeffdatum&id=10000-1187619796-S'
    driver.get(url)

    cookie_button = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, '[aria-label="Auswahl bestätigen – Ausgewählte Cookies werden akzeptiert"]')))
    
    cookie_button.click()

另一个问题是,当我尝试在无头模式下运行代码时,它正常运行的机会要低得多。

我正在使用“Google Chrome for Test”版本 124.0.6367.78 及其相应的 chromedriver。操作系统 Windows 10 Pro。

我已删除并重新下载浏览器和驱动程序。我已经检查了浏览器是否拥有所有必要的权限,这样它就不会被防火墙阻止,而且确实如此。

有人知道可能是什么问题吗?任何帮助将不胜感激。

python selenium-webdriver button selenium-chromedriver chrome-for-testing
1个回答
0
投票

目标元素位于shadow-root内。在这种情况下,您首先需要找到影子主机,然后找到到达目标元素的路径。

尝试此代码,这应该在无头或有头模式下始终单击目标元素(Auswahl bestätigen)。

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

chrome_options = Options()
chrome_options.add_argument('--headless')
driver = webdriver.Chrome(options=chrome_options)
url = 'https://www.arbeitsagentur.de/jobsuche/suche?angebotsart=34&was=Programmierer%2Fin&sort=veroeffdatum&id=10000-1187619796-S'
driver.get(url)
driver.maximize_window()
shadow_host = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.TAG_NAME, 'bahf-cookie-disclaimer-dpl3')))
shadow_root = shadow_host.shadow_root
cookie_button = WebDriverWait(shadow_root, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.ba-btn.ba-btn-contrast")))
driver.execute_script("arguments[0].click();", cookie_button)
© www.soinside.com 2019 - 2024. All rights reserved.