[英]Getting '403 Forbidden' error when using headless chrome with python selenium
使用常規 chromedriver 時,我的測試運行沒有任何問題,並且我得到了測試結果。 When I am trying to launch headless chrome with python and selenium, I get a '403 Forbidden' error on the screenshot and selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element:
on the console. 我通過了所有這些 arguments 仍然得到相同的結果:
chrome_options = Options()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--window-size=1920,1080')
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--allow-running-insecure-content')
chrome_options.add_argument("--headless")
driver = webdriver.Chrome(chrome_options=chrome_options)
我也嘗試了from fake_headers import Headers
但問題仍然存在
#!/usr/bin/python3
#try this
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium import webdriver
import time,os
from bs4 import BeautifulSoup
from pyvirtualdisplay import Display
from fake_useragent import UserAgent
def web(url):
display = Display(visible=0, size=(1920, 1080))
display.start()
ua = UserAgent()
userAgent = ua.chrome
chrome_options = Options()
chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
chrome_options.add_experimental_option('useAutomationExtension', False)
chrome_options.add_argument("--disable-blink-features=AutomationControlled")
chrome_options.add_argument(f'user-agent={userAgent}')
driver = webdriver.Chrome (options = chrome_options)
driver.get(url)
我找到了另一個類似於第一個答案的解決方案。可以在此處找到該問題的解釋。 這為我解決了這個問題。
chrome_options = Options()
user_agent = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.50 Safari/537.36'
chrome_options.add_argument(f'user-agent={user_agent}')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--window-size=1920,1080')
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--allow-running-insecure-content')
chrome_options.add_argument("--headless")
driver = webdriver.Chrome(chrome_options=chrome_options)
是的,您遇到了“用戶代理”問題,但建議對具有反機器人保護的 web 頁面使用 display(),盡管此解決方案是臨時的。 我的主要語言是西班牙語
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.