我设法使用 python 中的以下方法将 selenium 驱动程序连接到已打开的 chrome 会话
def iniciar_google_previa():
os.system('google-chrome --no-sandbox --remote-debugging-port=9222 --user-data-dir="/home/natanael/.config/google-chrome/Default" &')
time.sleep(2)
options1 = Options()
options1.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
chrome_driver = "/usr/local/bin/chromedriver"
driver = webdriver.Chrome(executable_path=ChromeDriverManager().install(), options=options1)
return driver
效果非常好,然后我可以使用
driver.get("site_url")
打开任何网站来开始抓取。
我想对 docker 容器进行同样的尝试。安装了 dockerfile 的所有要求,构建了它,然后尝试运行它,但出现以下错误
File "final1.py", line 104, in <module>
iniciar_ciclo()
File "final1.py", line 21, in iniciar_ciclo
driver = iniciar_google_previa()
File "final1.py", line 67, in iniciar_google_previa
driver = webdriver.Chrome(executable_path=ChromeDriverManager().install(), options=options1)
File "/opt/app-root/lib/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py", line 81, in __init__
desired_capabilities=desired_capabilities)
File "/opt/app-root/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "/opt/app-root/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/opt/app-root/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/opt/app-root/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, 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:9222
from unknown error: unable to discover open pages
我认为这是因为它正在尝试运行 GUI 所以我添加了
--headless
google-chrome --no-sandbox --headless --remote-debugging-port=9222 --user-data-dir="/home/natanael/.config/google-chrome/Default"
但遇到了同样的错误。
已经尝试安装
xvfb
然后
xvfb-run google-chrome --no-sandbox --remote-debugging-port=9222 --user-data-dir="/home/natanael/.config/google-chrome/Default"
给出了输出
DevTools listening on ws://127.0.0.1:9222/devtools/browser/52bbc92b-e096-4897-8661-233ee573edaf
显然它执行了 chrome 会话,但仍然无法连接到它,我不知道还能尝试什么
解释
我刚刚在 Mac 操作系统上遇到了同样的问题,我在其中创建了一个通过 SSH 从 iPhone 运行的程序。这意味着我从终端而不是 IDE 运行 Python 程序。由于我也是第一次获得另一个没有 9222 调试器端口选项的 URL,所以发生了一些特殊的事情:
options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
这导致终端连接到我正在运行的会话。因此,终端上方会弹出类似“Chrome Helper (Renderer) > main.py”的内容。它看起来像
但是当运行下面的命令时,你希望它看起来像这样
这似乎中断了我之后运行的命令:
string = "/Applications/Google\\ Chrome.app/Contents/MacOS/Google\\ Chrome --remote-debugging-port=9222"
kbd.write(string, 0.1)
sleep(5)
pyautogui.press('enter')
解决方案
这个对我有用的解决方案是打开一个新的终端窗口,然后我在其中输入相同的命令
"/Applications/Google\\ Chrome.app/Contents/MacOS/Google\\ Chrome --remote-debugging-port=9222"
这将打开与指定端口的会话。
这是通过使用 os.system 拼出打开该窗口的命令来完成的:
os.system('open -a Terminal -n')
sleep(3)
这样做之后,我打开了一个未连接到其他 Chrome 浏览器会话的新会话,这意味着终端上方的标题消失了!因此,我还能够使用前面提到的命令使用指定端口打开新的 Chrome 会话,而不会遇到相关错误!
示例代码:
import os
import pyautogui
from time import sleep
os.system('open -a Terminal -n')
sleep(3)
string = "/Applications/Google\\ Chrome.app/Contents/MacOS/Google\\ Chrome --remote-debugging-port=9222"
kbd.write(string, 0.1)
sleep(5)
pyautogui.press('enter')
sleep(7)
我希望这也适合你!
~ 刮刀小伙伴