當我在我的 PC 中嘗試 selenium 選項引數時,無論是模式(--headerless)還是(--silent),它都可以正常作業。但在客戶端設備上,它會以奇怪的代碼中斷。我什至在引數后添加了視窗大小,但錯誤是一樣的。Error with Feature-Policy header
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
chrome_options = Options()
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument('disable-notifications')
chrome_options.add_argument('--headerless')
webdriver_service = Service("chromedriver.exe")
driver = webdriver.Chrome(service=webdriver_service, options=chrome_options)
driver.get(url)
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "onetrust-accept-btn-handler"))).click()
uj5u.com熱心網友回復:
具有 selenium 無頭瀏覽器模式的網站被檢測為機器人。
避免檢測很大程度上取決于
maximize_window_size()在您的情況下,還需要添加
--disable-blink-features=AutomationControlled
以下示例在 Headless Chrome Selenium 中運行良好:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = Options()
options.headless = True
options.add_argument("start-maximized")
#options.add_experimental_option("detach", True)
options.add_argument("--no-sandbox")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('excludeSwitches', ['enable-logging'])
options.add_experimental_option('useAutomationExtension', False)
options.add_argument('--disable-blink-features=AutomationControlled')
webdriver_service = Service("./chromedriver") #Your chromedriver path
driver = webdriver.Chrome(service=webdriver_service,options=options)
url = 'https://soundcloud.com/daydoseofhouse/snt-whats-wrong/s-jmbaiBDyQ0d?si=233b2f843a2c4a7c8afd6b9161369717&utm\_source=clipboard&utm\_medium=text&utm\_campaign=social\_sharing'
driver.get(url)
cookie = WebDriverWait(driver, 15).until(EC.visibility_of_element_located((By.XPATH, '//*[@id="onetrust-accept-btn-handler"]'))).click()
video_duration = WebDriverWait(driver, 15).until(EC.visibility_of_element_located((By.XPATH, '//div[@]/span[2]'))).text
print(video_duration)
輸出:
2:51
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/516921.html
上一篇:將unsignedchar*轉換為std::shared_ptr<std::vector<unsignedchar>>
