Есть простой кусок кода Python:
#
from datetime import datetime
import undetected_chromedriver as uc
from xvfbwrapper import Xvfb
xvfb = Xvfb(width=800, height=600) # виртуальный экран
xvfb.start()
options = webdriver.ChromeOptions()
driver = uc.Chrome(options=options,service_args=["--verbose", "--log-path=\qc1.log"])
driver.get('https://example.com')
Данный код прекрасно работает на моей локальной машине под Linux Mint 20.3.
Но на VPS под Ubuntu 20.04 выдает следующую ошибку:
Traceback (most recent call last):
File "main.py", line 224, in <module>
driver = uc.Chrome(options=options,service_args=["--verbose", "--log-path=\qc1.log"])
File "/root/viza/env/viza_env/lib/python3.8/site-packages/undetected_chromedriver/__init__.py", line 401, in __init__
super(Chrome, self).__init__(
File "/root/viza/env/viza_env/lib/python3.8/site-packages/selenium/webdriver/chrome/webdriver.py", line 69, in __init__
super().__init__(DesiredCapabilities.CHROME['browserName'], "goog",
File "/root/viza/env/viza_env/lib/python3.8/site-packages/selenium/webdriver/chromium/webdriver.py", line 92, in __init__
super().__init__(
File "/root/viza/env/viza_env/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 276, in __init__
self.start_session(capabilities, browser_profile)
File "/root/viza/env/viza_env/lib/python3.8/site-packages/undetected_chromedriver/__init__.py", line 589, in start_session
super(selenium.webdriver.chrome.webdriver.WebDriver, self).start_session(
File "/root/viza/env/viza_env/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 369, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/root/viza/env/viza_env/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 434, in execute
self.error_handler.check_response(response)
File "/root/viza/env/viza_env/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 243, 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:57609
from chrome not reachable
Stacktrace:
#0 0x559237183693 <unknown>
#1 0x559236f7c9db <unknown>
#2 0x559236f6c81e <unknown>
#3 0x559236fa5677 <unknown>
#4 0x559236f9ce9f <unknown>
#5 0x559236fd8953 <unknown>
#6 0x559236fd2743 <unknown>
#7 0x559236fa8533 <unknown>
#8 0x559236fa9715 <unknown>
#9 0x5592371d37bd <unknown>
#10 0x5592371d6bf9 <unknown>
#11 0x5592371b8f2e <unknown>
#12 0x5592371d79b3 <unknown>
#13 0x5592371ace4f <unknown>
#14 0x5592371f6ea8 <unknown>
#15 0x5592371f7052 <unknown>
#16 0x55923721171f <unknown>
#17 0x7f2fbdc56609 <unknown>
Дело явно в связке selenium и Xvfb, т.к если убрать Xvfb и добавить в options '--headless', то всё заработает нормально(т.е selenium и undetected_chromedriver работают нормально ). Но необходимо использовать именно виртуальный монитор Xvfb.
Используемые версии(на локальной машине и VPS одинаковые версии):
Python -3.8.10
Google Chrome -105.0.5195.102
xvfbwrapper-0.2.9
selenium-4.4.0
undetected-chromedriver- 3.1.5.post4
Логи хрома:
[1663022031.301][INFO]: Starting ChromeDriver 105.0.5195.52 (412c95e518836d8a7d97250d62b29c2ae6a26a85-refs/branch-heads/5195@{#853}) on port 39881
[1663022031.301][INFO]: Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
[1663022031.803][INFO]: [015f206e73571679cf3dd2c348d7d167] COMMAND InitSession {
"capabilities": {
"alwaysMatch": {
"browserName": "chrome",
"goog:chromeOptions": {
"args": [ "--remote-debugging-host=127.0.0.1", "--remote-debugging-port=54145", "--user-data-dir=/tmp/tmpb00p2f2g", "--lang=en-US", "--no-default-browser-check", "--no-first-run", "--log-level=0" ],
"binary": "/usr/bin/google-chrome",
"debuggerAddress": "127.0.0.1:54145",
"extensions": [ ]
},
"pageLoadStrategy": "normal"
},
"firstMatch": [ {
} ]
}
}
[1663022031.804][DEBUG]: DevTools HTTP Request: http://127.0.0.1:54145/json/version
[1663022031.805][DEBUG]: DevTools HTTP Request failed
[1663022031.855][DEBUG]: DevTools HTTP Request: http://127.0.0.1:54145/json/version
[1663022031.856][DEBUG]: DevTools HTTP Request failed
..............
..............
[1663022091.816][INFO]: [015f206e73571679cf3dd2c348d7d167] RESPONSE InitSession ERROR unknown error: cannot connect to chrome at 127.0.0.1:54145
from chrome not reachable
Может кто-то сталкивался с похожей проблемой?