在 Python 中使用未检测到的 chromedriver 时,无法连接到 127.0.0.1:37541 上的 chrome
使用 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: Message: unknown error: cannot connect to chrome at 127.0.0.1:37541 from chrome not reachable
使用常规 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: Message: unknown error: cannot connect to chrome at 127.0.0.1:42305 from chrome not reachable
检查路径是否存在
# 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: Message: unknown error: cannot connect to chrome at 127.0.0.1:42467 from unknown error: unable to discover open pages (Driver info: chromedriver=2.41.578700 (2f1ed5f9343c13f73144538f15c00b370eda6706),platform=Linux 5.11.0-18-generic x86_64)
我在 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")
希望这对某些人有帮助,因为我浪费了好几天才找到这个解决方案!!:-)
删除配置文件路径中的所有内容。它对我有用
我通过手动运行
chrome.exe --user-data-dir="userdata 文件夹路径"
解决了这个问题。
请参阅此
github 链接