在使用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从无法到达的铬连接到
使用普通硒是没有问题的
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从无法到达的铬连接到
检查路径是否存在
# ll /usr/bin/chromedriver
-rwxr-xr-x 1 root root 8298464 Oct 1 14:19 /usr/bin/chromedriver*
第二次尝试
尝试使用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连接到铬从未知错误:无法发现打开的页面(驱动程序信息: chromedriver=2.41.578700 (2f1ed5f9343c13f731445f15c00b370eda6706),platform=Linux 5.11.0-18-泛型x86_64)
发布于 2022-01-14 00:47:38
在Centos 7中,我也遇到了同样的问题,问题是铬的版本没有被检测到--铬和色度驱动器没有对齐。
适用于我的解决方案(我使用Pythonv3.8):
cd /YOUR_CHROMEDRIVER_PATH yum删除google-/YOUR_CHROMEDRIVER_PATH* python3.8 -m pip卸载未检测到- chromedriver -f色驱动器
python3.8 -m pip安装未被检测-chromedriver==3.0.6wget -N http://chromedriver.storage.googleapis.com/96.0.4664.35/chromedriver_linux64.zip unzip chromedriver_linux64.zip yum -y chromedriver_linux64.zip 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稳定/bin/chrome
从SeleniumImportWebdriverchrome_options= webdriver.ChromeOptions() chrome_options.add_argument('--no-sandbox') chrome_options.add_argument(‘--无头’)chrome_options.add_argument chrome_options.add_argument(‘-disable’) user_agent = 'Mozilla/5.0 (Macintosh;英特尔Mac 10_15_7) AppleWebKit/605.1.15 (KHTML,类似Gecko)版本/15.0 Safari/605.1.15‘chrome_options.add_experimental_option('useAutomationExtension',True)驱动程序= uc.Chrome(executable_path='/path_to_your_driver/chromedriver',chrome_options=chrome_options,driver.implicitly_wait(6.5) driver.get(“https://google.com"”)
希望这对某人有帮助,因为我浪费了几天时间来找到这个解决方案!
发布于 2021-12-15 08:53:19
删除配置文件中的所有内容path.it对我有用
https://stackoverflow.com/questions/69407211
复制