gpt4 book ai didi

google-chrome-headless - headless Chrome 未检测到 css 元素

转载 作者:行者123 更新时间:2023-12-04 19:33:18 24 4
gpt4 key购买 nike

我发现 headless Chrome 似乎没有检测到元素。以下脚本不起作用。

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

options = Options()
options.add_argument("--headless")
driver = webdriver.Chrome(chrome_options=options)
driver.set_window_size(1024, 600)
driver.maximize_window()

driver.get('https://www.sportsbet.com.au/betting/soccer?LeftNav')

wait = WebDriverWait(driver, 10)
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".link")))
print('hi')

改为非 headless 。
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait

driver = webdriver.Chrome()
driver.set_window_size(1024, 600)
driver.maximize_window()

driver.get('https://www.sportsbet.com.au/betting/soccer?LeftNav')

wait = WebDriverWait(driver, 10)
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".link")))
print('test2')

成功!

我得到了 headless :
line 49, in <module>
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".link")))
File "C:\Users\Django\AppData\Local\Continuum\miniconda3\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:

更改 css:div:nth-child(1) > a > span.team-name.ib

我仍然得到同样的错误。为什么要这样做?

最佳答案

如果您使用的是 headless 浏览器,它似乎会重定向您。

添加:

user_agent = 'I LIKE CHOCOLATE'
options.add_argument(f'user-agent={user_agent}')

现在运行:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait

options = Options()
options.add_argument("--headless")
user_agent = 'I LIKE CHOCOLATE'
options.add_argument(f'user-agent={user_agent}')
driver = webdriver.Chrome(chrome_options=options)
driver.set_window_size(1024, 600)
options.add_argument("--headless");
driver.maximize_window()

driver.get('https://www.sportsbet.com.au/betting/soccer?LeftNav')

wait = WebDriverWait(driver, 10)
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".link")))
print('hi')

成功!

关于google-chrome-headless - headless Chrome 未检测到 css 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47900496/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com