首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无头模式与-silent模式的硒差异

无头模式与-silent模式的硒差异
EN

Stack Overflow用户
提问于 2022-10-17 08:16:14
回答 1查看 51关注 0票数 -1

当我在我的PC中尝试selenium选项参数时,无论是使用哪种模式(--无头模式)还是(--沉默模式),都可以正常工作。但在客户端设备上,它会用奇怪的代码中断。我甚至在参数之后添加了窗口大小,但是错误是一样的。

代码语言:javascript
复制
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()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-10-17 14:19:17

  1. 采用selenium无头浏览器模式检测为bot.

的网站

maximize_window_size()在很大程度上依赖于

  1. 来避免检测。

在您的例子中,还需要添加--disable-blink-features=AutomationControlled

下面的示例在无头铬硒中运行良好:

代码语言:javascript
复制
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%5C_source=clipboard&utm%5C_medium=text&utm%5C_campaign=social%5C_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[@class="playbackTimeline__duration sc-text-primary sc-text-h5"]/span[2]'))).text

print(video_duration)

输出:

代码语言:javascript
复制
2:51
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74094336

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档