Headless:Ubuntu / Chrome / Selenium?

vom3gejh  于 2023-09-28  发布在  Go
关注(0)|答案(1)|浏览(106)

我试图在Ubuntu VDS上运行一个简单的Python代码

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
 
options = Options()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
 
driver.get("https://python.org")
print(driver.title)
driver.close()

在运行它之前,我在shh连接后做了以下操作。

sudo apt update
sudo apt upgrade

安装的Chrome

wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
sudo dpkg -i google-chrome-stable_current_amd64.deb
sudo apt-get install -f

安装selenium和webdriver_manager

sudo apt install python3-pip
pip install selenium
pip install webdriver_manager

不幸的是,在运行上面的python代码后,我看到下面的错误:

Traceback (most recent call last):
  File "/root/test.py", line 24, in <module>
    driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
  File "/root/venv/lib/python3.10/site-packages/webdriver_manager/chrome.py", line 40, in install
    driver_path = self._get_driver_binary_path(self.driver)
  File "/root/venv/lib/python3.10/site-packages/webdriver_manager/core/manager.py", line 40, in _get_driver_binary_path
    file = self._download_manager.download_file(driver.get_driver_download_url(os_type))
  File "/root/venv/lib/python3.10/site-packages/webdriver_manager/core/download_manager.py", line 29, in download_file
    response = self._http_client.get(url)
  File "/root/venv/lib/python3.10/site-packages/webdriver_manager/core/http.py", line 36, in get
    self.validate_response(resp)
  File "/root/venv/lib/python3.10/site-packages/webdriver_manager/core/http.py", line 19, in validate_response
    raise ValueError(
ValueError: response body:
Google-Edge-Cache: forbidden
Error: 7
request url:
https://edgedl.me.gvt1.com/edgedl/chrome/chrome-for-testing/116.0.5845.96/linux64/chromedriver-linux64.zip
response headers:
{'content-length': '37', 'content-type': 'text/plain', 'x-request-id': '6bf004ec-4d9e-4bd0-a5d6-a296a27fc6db', 'alt-svc': 'h3=":443"; ma=2592000, h3-29=":443"; ma=2592000', 'date': 'Mon, 21 Aug 2023 08:24:16 GMT', 'server': 'Google-Edge-Cache'}

尝试运行无头python selenium代码

w8biq8rn

w8biq8rn1#

假设您使用的是最新的selenium版本(v4.11.2)。删除代码的webdriver_manager部分。Selenium的内置工具将为您下载和管理驱动程序,不再需要WDM
验证码:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome(options=options)

driver.get("https://python.org")
print(driver.title)
driver.close()

控制台输出:

Welcome to Python.org

Process finished with exit code 0

相关问题