5

使用 Selenium 后,我决定尝试undetected-chromedriver使用

 pip install undetected-chromedriver

但是,运行这个简单的脚本

import undetected_chromedriver.v2 as uc

options = uc.ChromeOptions()
options.add_argument('--no-sandbox')

driver = uc.Chrome(options=options)
with driver:
    driver.get('https://google.com') 

给出错误

selenium.common.exceptions.WebDriverException:消息:未知错误:无法从 chrome 连接到 127.0.0.1:37541 的 chrome 无法访问

使用常规 Selenium 没有问题

from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument("--no-sandbox"); # Bypass OS security model

driver = webdriver.Chrome(options=options)
with driver:
    driver.get('https://google.com') 

这是回溯

Traceback (most recent call last):
  File "/root/test-bot/src/test.py", line 6, in <module>
    driver = uc.Chrome()
  File "/root/anaconda3/envs/test/lib/python3.9/site-packages/undetected_chromedriver/v2.py", line 302, in __init__
    super(Chrome, self).__init__(
  File "/root/anaconda3/envs/test/lib/python3.9/site-packages/selenium/webdriver/chrome/webdriver.py", line 69, in __init__
    super(WebDriver, self).__init__(DesiredCapabilities.CHROME['browserName'], "goog",
  File "/root/anaconda3/envs/test/lib/python3.9/site-packages/selenium/webdriver/chromium/webdriver.py", line 93, in __init__
    RemoteWebDriver.__init__(
  File "/root/anaconda3/envs/test/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 248, in __init__
    self.start_session(capabilities, browser_profile)
  File "/root/anaconda3/envs/test/lib/python3.9/site-packages/undetected_chromedriver/v2.py", line 577, in start_session
    super(Chrome, self).start_session(capabilities, browser_profile)
  File "/root/anaconda3/envs/test/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 339, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/root/anaconda3/envs/test/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 400, in execute
    self.error_handler.check_response(response)
  File "/root/anaconda3/envs/test/lib/python3.9/site-packages/selenium/webdriver/remote/errorhandler.py", line 236, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: cannot connect to chrome at 127.0.0.1:52681
from chrome not reachable
  (Driver info: chromedriver=2.41.578700 (2f1ed5f9343c13f73144538f15c00b370eda6706),platform=Linux 5.11.0-18-generic x86_64)

有什么建议么?


尝试#1

还尝试设置executable_path/usr/bin/chromedriver

import undetected_chromedriver.v2 as uc

options = uc.ChromeOptions()
options.add_argument('--no-first-run --no-service-autorun --password-store=basic')

CHROME_DRIVER_PATH = '/usr/bin/chromedriver'
driver = uc.Chrome(executable_path=CHROME_DRIVER_PATH, options=options)
with driver:
    driver.get('https://google.com') 

这给出了相同的错误

selenium.common.exceptions.WebDriverException:消息:未知错误:无法从 chrome 连接到 127.0.0.1:42305 的 chrome 无法访问

检查路径是否存在

# ll /usr/bin/chromedriver
-rwxr-xr-x 1 root root 8298464 Oct  1 14:19 /usr/bin/chromedriver*

尝试#2

尝试使用 Xvfb 并禁用无头模式

import undetected_chromedriver.v2 as uc
from xvfbwrapper import Xvfb
vdisplay = Xvfb(width=800, height=1280)
vdisplay.start()

options = uc.ChromeOptions()
options.add_argument('--no-first-run --no-service-autorun --password-store=basic')
options.user_data_dir = f'./tmp/test_undetected_chromedriver'
options.add_argument(f'--disable-gpu')
options.add_argument(f'--no-sandbox')
options.add_argument(f'--disable-dev-shm-usage')

CHROME_DRIVER_PATH = '/usr/bin/chromedriver'
driver = uc.Chrome(executable_path=CHROME_DRIVER_PATH, options=options, headless=False)
with driver:
    driver.get('https://google.com') 
    print(driver.title)

略有不同的错误

selenium.common.exceptions.WebDriverException:消息:未知错误:无法从未知错误连接到 127.0.0.1:42467 的 chrome:无法发现打开的页面(驱动程序信息:chromedriver=2.41.578700(2f1ed5f9343c13f73144538f15c00b370eda6706),平台=Linux 5.11。 0-18-通用 x86_64)

4

3 回答 3

1

我在 Centos 7 中遇到了完全相同的问题,问题是未对齐的 chrome、undetected-chrome 和 chromedriver 版本。

对我有用的解决方案(我使用的是 Python v3.8):

  1. 删除现有版本
cd /YOUR_CHROMEDRIVER_PATH
yum remove google-chrome*
python3.8 -m pip uninstall undetected-chromedriver
rm -f chromedriver
  1. 安装对齐的版本
python3.8 -m pip install undetected-chromedriver==3.0.6
wget -N http://chromedriver.storage.googleapis.com/96.0.4664.35/chromedriver_linux64.zip
unzip chromedriver_linux64.zip
yum -y install https://dl.google.com/linux/chrome/rpm/stable/x86_64/google-chrome-stable-96.0.4664.110-1.x86_64.rpm
ln -s /usr/bin/google-chrome-stable /bin/chrome
  1. 我的工作 python 脚本示例:

import undetected_chromedriver as uc
from selenium import webdriver

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--headless')
chrome_options.add_argument('--enable-javascript')
chrome_options.add_argument('--disable-gpu')
user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.0 Safari/605.1.15'
chrome_options.add_argument('User-Agent={0}'.format(user_agent))
chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
chrome_options.add_experimental_option('useAutomationExtension', True)

driver = uc.Chrome(executable_path='/path_to_your_driver/chromedriver',chrome_options=chrome_options,service_args=['--quiet'])
driver.implicitly_wait(6.5)

driver.get("https://google.com")

希望这可以帮助某人,因为我浪费了几天时间来找到这个解决方案!:-)

于 2022-01-14T08:47:38.763 回答
0

删除配置文件路径中的所有内容。它对我有用

于 2021-12-15T16:53:19.570 回答
0

您确定默认的未检测到的 chromedriver 本身是无头的吗?请尝试使其无头。一个有类似错误的人已经修复了它https://github.com/ultrafunkamsterdam/undetected-chromedriver/issues/302

但请注意,在未检测到的 chromedriver 中仍然不完全支持无头,并且谷歌登录会检测到它:(

当您有修复时请告诉我们

于 2022-03-06T02:21:41.520 回答