首页 > 解决方案 > 如何配置 Chrome 浏览器以无头模式运行

问题描述

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

from time import sleep

# Gumtree credentials
username = "my username"
password = "my password"

# Removes SSL Issues With Chrome
options = webdriver.ChromeOptions()
options.add_argument('--ignore-certificate-errors')
options.add_argument('--ignore-ssl-errors')
options.add_argument('--ignore-certificate-errors-spki-list')
options.add_argument('log-level=3') 
options.add_argument('--disable-notifications')

# Initiate Headless Chrome
#options.add_argument('--headless')
#options.add_argument('--disable-gpu')
#options.headless = True

# Initiate Chrome Driver
url = 'https://my.gumtree.com/login'
driver = webdriver.Chrome(executable_path="C:\webdrivers\chromedriver.exe",options=options)
driver.get(url)

# Find Username/Email Field and Send to Input Field
driver.find_element_by_id("email").send_keys(username)
# Find Password Field and Send to Input Field
driver.find_element_by_id("fld-password").send_keys(password)

# Initiate Consent Button
consent_button_xpath = '//*[@id="login-form"]/div/form/fieldset/div[3]/button'
consent = WebDriverWait(driver, 40).until(EC.element_to_be_clickable((By.XPATH, consent_button_xpath)))
consent = driver.find_element_by_xpath(consent_button_xpath)
consent.click()

# Print Username to Test Successful Login
login_username = driver.find_element_by_xpath('//*[@id="is-being-refined"]/div[3]/div/header/div[1]/div/nav/div/ul/li[5]/a/div').text
print(login_username)

# close the driver
driver.close()

上面的脚本使用Selenium成功地自动登录到一个网站并打印用户名。

问题

我正在尝试使用Headless Chrome以无声模式执行上述操作,但没有任何乐趣。
我研究了很多文章,只需添加开关options.add_argument('--headless')会导致脚本失败。

任何帮助将非常感激。

错误信息

[0830/063818.313:ERROR:ssl_client_socket_impl.cc(981)] handshake failed; returned -1, SSL error code 1, net_error -101
[0830/063818.313:ERROR:ssl_client_socket_impl.cc(981)] handshake failed; returned -1, SSL error code 1, net_error -101
Traceback (most recent call last):
  File "e:\Python Projects\Gumtree\test5.py", line 39, in <module>
    consent.click()
  File "C:\Python39\lib\site-packages\selenium\webdriver\remote\webelement.py", line 80, in click
    self._execute(Command.CLICK_ELEMENT)
  File "C:\Python39\lib\site-packages\selenium\webdriver\remote\webelement.py", line 633, in _execute
    return self._parent.execute(command, params)
  File "C:\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Python39\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element <button class="btn-primary btn-full-width" type="submit" data-analytics="gaEvent:...Attempt,userData:{lip:Email}">Login</button> is not clickable at point (386, 564). Other element would receive the click: <button id="onetrust-pc-btn-handler" tabindex="0" class="cookie-setting-link">...</button>
  (Session info: headless chrome=92.0.4515.159)

标签: pythonseleniumselenium-webdriverheadless-browser

解决方案


这个错误:

selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element Login is not clickable at point (386, 564). Other element would receive the click: ... (Session info: headless chrome=92.0.4515.159)

与硒没有切换到无头模式没有任何关系。

你写作的那一刻

options.add_argument("--headless")

并在 webdriver 对象创建调用中传递此选项引用,Selenium 会将其视为无头模式。

  1. 我也建议explicit waits/Implicit waits在您认为需要时使用。

  2. Headless 从来都不是稳定的,到处都会引起问题。如果您只想进行网络抓取,请考虑使用不同的工具,例如BS4或请求模块。

  3. 此外,您应该使用relative xpath而不是绝对 xpath


推荐阅读