如何配置 Chrome 浏览器以无头模式运行

马克WP
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)

杨贴东

我建议不要使用鼠标单击提交按钮,而是使用键盘:

from selenium.webdriver.common.keys import Keys
consent.send_keys(Keys.ENTER)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何配置ChromeDriver通过Selenium以无头模式启动Chrome浏览器?

如何设置IE浏览器在无头模式下运行的功能

如何使用谷歌浏览器在无头模式下运行 botium-cli

如何设置窗口大小,全屏使用Chrome浏览器选项无头镶边?

如何在Cypress中将默认的无头浏览器更改为Chrome

如何使用无头浏览器运行测试?

如何使用无头浏览器运行Selenium Python

无头Chrome元素不可点击(在浏览器模式下工作)

如何使用单独的(用户 + 配置文件)运行两个 chrome 浏览器实例

使用Chrome无头浏览器获取渲染字体

无法下载无头Chrome浏览器中的文件

以无头模式运行Chrome

使Chrome以无头模式运行

如何在Chrome浏览器中运行硒测试?

如何在Google Chrome浏览器中运行Postman

如何运行 Chrome 工具而不是浏览器

如何使用插件在 Chrome 浏览器中运行 apk?

在Linux上使用无头Chrome浏览器访问“拒绝”页面,而带头Chrome浏览器在Windows上使用Selenium通过Python在Windows上运行

如何在Rails 5.1中运行无头浏览器系统测试?

如何运行无头硒脚本,独立于主机和浏览器

与正常模式相比,以无头模式运行Chrome如何影响硒测试?

Xlib:在显示“:21”上缺少扩展名“ RANDR”。-尝试运行无头的Google Chrome浏览器

如何将Google Chrome浏览器作为两个唯一的Google Chrome浏览器运行?

如何在无头模式或后台运行chrome驱动程序

如何使Google Chrome浏览器的“应用程序模式”在外部浏览器中打开外部链接?

如何以无头模式启动铬浏览器?显示屏“:99”缺少扩展名“ RANDR”

如何不使用Selenium服务器而从量角器无头运行Chrome

如何使Chrome浏览器更像Firefox?

如何从Java启动Chrome浏览器