Selenium XVFB - 无法从渲染器接收消息

Selenium XVFB - 无法从渲染器接收消息

概述:

刮刀工作完美在无头模式下。产卵一个虚拟显示没有显示错误通过XVFB:

from xvfbwrapper import Xvfb

vdisplay = Xvfb()
vdisplay.start()
vdisplay.stop()

但当我尝试将它们一起运行,但会出错和:

[错误] SessionNotCreatedException:消息:会话未从断开连接创建:无法从渲染器接收消息(会话信息:chrome=96.0.4664.0)

追溯:

Traceback (most recent call last):
  File "/var/task/slack_main.py", line 34, in handler
    scrape_price(asin_list)
  File "/var/task/slack_main.py", line 58, in scrape_price
    driver = webdriver.Chrome("/opt/chromedriver",options=options)
  File "/var/lang/lib/python3.9/site-packages/selenium/webdriver/chrome/webdriver.py", line 70, in __init__
    super(WebDriver, self).__init__(DesiredCapabilities.CHROME['browserName'], "goog",
  File "/var/lang/lib/python3.9/site-packages/selenium/webdriver/chromium/webdriver.py", line 92, in __init__
    RemoteWebDriver.__init__(
  File "/var/lang/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 275, in __init__
    self.start_session(capabilities, browser_profile)
  File "/var/lang/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 365, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/var/lang/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 430, in execute
    self.error_handler.check_response(response)
  File "/var/lang/lib/python3.9/site-packages/selenium/webdriver/remote/errorhandler.py", line 247, in check_response
    raise exception_class(message, screen, stacktrace)

配置:

以下是我完整的 Selenium 和 XVFB 配置:

from selenium import webdriver
from selenium_stealth import stealth
from xvfbwrapper import Xvfb

vdisplay = Xvfb()
vdisplay.start()
options = webdriver.ChromeOptions()
prefs = {"browser.downloads.dir": "//tmp//", "download.default_directory": "//tmp//", "directory_upgrade": True}
options.add_experimental_option("prefs", prefs)
options.binary_location = '/opt/chrome/chrome'
#options.add_argument('--headless') #toggled on and off when running with or without XVFB
options.add_argument('--no-sandbox')
options.add_argument("--disable-gpu")
options.add_argument("--window-size=1280x1696")
options.add_argument("--single-process")
options.add_argument("--disable-dev-shm-usage")
options.add_argument("--disable-dev-tools")
options.add_argument("--no-zygote")
options.set_capability('unhandledPromptBehavior', 'ignore')
options.add_argument("download.default_directory=/tmp")
driver = webdriver.Chrome("/opt/chromedriver",options=options)
stealth(driver,
        languages=["en-US", "en"],
        vendor="Google Inc.",
        platform="Win32",
        fix_hairline=True,
        )
vdisplay.stop()
driver.close()

为什么连接不上显示器?我猜这与“--headless”切换有关?

版本和工具:

  • 硒版本 3.141.0
  • xvfbwrapper 版本 0.2.9
  • Docker用于编译并推送到AWS Lambda,使用的基础图像 (无论有没有 XVFB,docker 文件都不会发生任何变化)

相关内容