gpt4 book ai didi

Selenium 只能在非 headless (headless)模式下工作吗?

转载 作者:行者123 更新时间:2023-12-04 07:25:02 28 4
gpt4 key购买 nike

我想在这个页面上使用 Selenium:https://www.avis.com/en/home
如果没有 headless (headless)模式,该代码一切正常:

import requests
from bs4 import BeautifulSoup
import os, sys, time
import xlwings as xw
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.action_chains import ActionChains
from random import choice
from selenium import webdriver
from sys import platform

WAIT = 1
link = f"https://www.avis.com/en/home"
cd = '/chromedriver.exe'
options = Options()
# options.add_argument('--headless')
options.add_experimental_option ('excludeSwitches', ['enable-logging'])
options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36")
driver = webdriver.Chrome (path + cd, options=options)
driver.get (link)
print(f"Read location VIE...")
driver.find_element_by_id("PicLoc_value").send_keys("VIE")
driver.find_element_by_id("PicLoc_value").click()
time.sleep(WAIT)
print(f"Read from date 2021-07-06...")
driver.find_element_by_id("from").clear()
driver.find_element_by_id("from").click()
driver.find_element_by_id("from").send_keys("07/06/2021")
time.sleep(WAIT)
print(f"Read to date 2021-07-21...")
driver.find_element_by_id("to").clear()
driver.find_element_by_id("to").click()
driver.find_element_by_id("to").send_keys("07/21/2021")
time.sleep(WAIT)
driver.find_element_by_id("res-home-select-car").click()
但是当我取消注释该行时 options.add_argument('--headless')它不再工作了
我总是收到此错误消息:
Read location VIE...
Read from date 2021-07-06...
Read to date 2021-07-21...
Traceback (most recent call last):
File "C:\Users\Polzi\Documents\DEV\Upwork\JeffG\scrapeAvis.py", line 59, in <module>
driver.find_element_by_id("res-home-select-car").click()
File "C:\Users\Polzi\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webelement.py", line 80, in click
self._execute(Command.CLICK_ELEMENT)
File "C:\Users\Polzi\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webelement.py", line 633, in _execute
return self._parent.execute(command, params)
File "C:\Users\Polzi\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\Polzi\AppData\Local\Programs\Python\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 name="button" id="res-home-select-car" ng-class="{'margin-top-90':false &amp;&amp; vm.expandState &amp;&amp; isAuthenticated}" class="btn btn-red selectMyCar hideMeFix btn-primary-avis" ng-if="!vm.isOneClick" ng-mousedown="vm.selectCarClicked = true" ng-click="vm.getVehicles.submit(resForm)">...</button> is not clickable at point (391, 417). Other element would receive the click: <div class="flyoutWrapper background_img suppress-lazyloading" imgsrc="/content/dam/avis/na/us/common/offers/avis-redbackground-600x250.jpg/jcr:content/renditions/cq5dam.web.768.504.webp" request="background" style="background-image: url(&quot;/content/dam/avis/na/us/common/offers/avis-redbackground-600x250.jpg/jcr:content/renditions/cq5dam.web.768.504.webp&quot;); opacity: 1;">...</div>
(Session info: headless chrome=91.0.4472.124)
为什么这里 selenium 只能在没有 headless (headless)模式的情况下工作?

最佳答案

使用 headless 模式时,您应该设置屏幕尺寸,因为 headless 模式下的默认屏幕尺寸为 800x600。
这就是为什么在 headless (headless)模式下你有 ElementClickInterceptedException在常规模式下运行良好的代码的错误。
所以,尝试添加

options.add_argument("--window-size=1920x1080")
此外,我们通常会添加以下内容:
options.add_argument('--no-sandbox')
options.add_argument('--disable-gpu')

关于Selenium 只能在非 headless (headless)模式下工作吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68262011/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com