Selenium Headless Chrome 中的元素加载问题

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

在无头 Chrome 中运行 Selenium WebDriver 时,即使引入延迟并调整许多 ChromeOptions 之后,我也无法加载页面。这是我尝试过的多种配置之一:

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
import time

options = Options()
options.add_argument("--headless")
options.add_argument("--disable-gpu")
options.add_argument("--window-size=1920,1080")
options.add_argument("--disable-blink-features=AutomationControlled")
driver = webdriver.Chrome(options=options)
driver.get("https://www.example.com")
time.sleep(5)
wait = WebDriverWait(driver, 30)
try:
    wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "some-selector")))
except Exception as e:
    print("Exception caught:", e)

当我忽略无头模式时,一切都会按预期工作,但无论我使用什么标志和超时,页面都不会加载。难道是我尝试访问的特定页面有问题吗? (我尝试了很多)

selenium-webdriver selenium-chromedriver
1个回答
0
投票

看起来您正在使用旧的无头模式。 使用 Chrome 更新的无头模式:

options.add_argument("--headless=new")

这使得 Chrome 的无头模式获得与常规 Chrome 相同的结果。

© www.soinside.com 2019 - 2024. All rights reserved.