无法使用 python 启动 selenium 会话

not able to start the session for the selenium with python

我在 运行ning 简单的 selenium python 自动化程序无法解决主要问题时遇到问题。

问题要么出在 chrome 驱动程序上,要么找不到根本原因。

我已经在我的 Linux 机器上安装了 google-chrome 和 chrome-驱动程序,

linux 机器:Linux 5.4.0-73-通用 #82~18.04.1-Ubuntu

安装google

sudo apt-get -y install google-chrome-stable

安装 chormedriver

wget -N https://chromedriver.storage.googleapis.com/90.0.4430.24/chromedriver_linux64.zip

之后执行下面的代码。

from selenium import webdriver
from selenium.webdriver.chrome.options import Options


chromeOptions = Options()
chromeOptions.headless = True
chromeOptions.add_argument("--remote-debugging-port=9222")
chromeOptions.add_argument("--no-sandbox")
chromeOptions.add_argument("--disable-setuid-sandbox")

driver = webdriver.Chrome(executable_path='Application/chromedriver',options=chromeOptions)

# Navigate to page
driver.get('https://google.com')
print(driver.title)

driver.quit()

之后收到以下错误

Traceback (most recent call last):
File "sly_test.py", line 12, in <module>
driver = webdriver.Chrome(executable_path='Application/chromedriver',options=chromeOptions)
File "/home/sysadmin/.local/lib/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py", line 81, in __init__
desired_capabilities=desired_capabilities)
File "/home/sysadmin/.local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "/home/sysadmin/.local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/home/sysadmin/.local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/home/sysadmin/.local/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.SessionNotCreatedException: Message: session not created
from disconnected: unable to connect to renderer
(Session info: headless chrome=90.0.4430.212)

经过一些努力和帮助,我尝试卸载所有内容并使用此 link 中的 install.sh 文件重新安装。后来才知道我的 chrome 旧进程正在进行,这就是为什么它无法创建新会话并抛出错误 Message: Session not created。 最后,使用 pkill -9 -f chrom 终止 chrome 的所有进程,能够使用 python 代码执行 selenium 而不会出现任何错误。