`cannot connect to chrome at 127.0.0.1:37541` when using undetected-chromedriver with Python

Posted

技术标签:

【中文标题】`cannot connect to chrome at 127.0.0.1:37541` when using undetected-chromedriver with Python【英文标题】: 【发布时间】:2021-11-23 04:19:44 【问题描述】:

使用 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:消息:未知错误:无法在 127.0.0.1:37541 连接到 chrome 从 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:消息:未知错误:无法在 127.0.0.1:42305 连接到 chrome 从 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),platform=Linux 5.11.0-18-generic x86_64)

【问题讨论】:

为您的司机尝试set up logging 并在那里找到线索。 【参考方案1】:

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

【讨论】:

【参考方案2】:

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

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

    删除现有版本
cd /YOUR_CHROMEDRIVER_PATH
yum remove google-chrome*
python3.8 -m pip uninstall undetected-chromedriver
rm -f chromedriver
    安装对齐的版本
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
    我的工作 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")

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

【讨论】:

以上是关于`cannot connect to chrome at 127.0.0.1:37541` when using undetected-chromedriver with Python的主要内容,如果未能解决你的问题,请参考以下文章

cannot connect to daemon at tcp:5037: cannot connect to 127.0.0.1:5037: 由于目标计算机积极拒绝,无法连接。 (10061)(示例

cannot connect to IP: 由于目标计算机积极拒绝,无法连接。 (10061)

centos 环境正确配置 wkhtmltopdf:cannot connect to X server

error: cannot connect to daemon解决办法

unable to connect to 127.0.0.1:62001: cannot connect to 127.0.0.1:62001: 由于目标计算机积极拒绝,无法连接。 (10061)

adb 运行提示error: cannot connect to daemon