为什么在Ubuntu 18.04上运行Selenium chrome驱动程序时无法启动chrome浏览器 [英] Why chrome browser doesn't launch when running Selenium chrome driver on Ubuntu 18.04
问题描述
当我尝试使用Selenium库和chromedriver运行python程序时遇到各种错误.我按照Selenium网站上的说明进行安装,然后安装我的前两行:
I am getting various errors when I'm trying to run a python program using Selenium library and chromedriver. I follow the instructions on Selenium website to install all what I needed and then start programming my two first lines :
from selenium.webdriver import Chrome
driver = Chrome()
我收到此错误消息:
WebDriverException Traceback (most recent call last)
<ipython-input-16-dce6fb94cc37> in <module>
1 from selenium.webdriver import Chrome
2
----> 3 driver = Chrome()
~/anaconda3/lib/python3.7/site-packages/selenium/webdriver/chrome/webdriver.py in __init__(self, executable_path, port, options, service_args, desired_capabilities, service_log_path, chrome_options, keep_alive)
79 remote_server_addr=self.service.service_url,
80 keep_alive=keep_alive),
---> 81 desired_capabilities=desired_capabilities)
82 except Exception:
83 self.quit()
~/anaconda3/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py in __init__(self, command_executor, desired_capabilities, browser_profile, proxy, keep_alive, file_detector, options)
155 warnings.warn("Please use FirefoxOptions to set browser profile",
156 DeprecationWarning, stacklevel=2)
--> 157 self.start_session(capabilities, browser_profile)
158 self._switch_to = SwitchTo(self)
159 self._mobile = Mobile(self)
~/anaconda3/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py in start_session(self, capabilities, browser_profile)
250 parameters = {"capabilities": w3c_caps,
251 "desiredCapabilities": capabilities}
--> 252 response = self.execute(Command.NEW_SESSION, parameters)
253 if 'sessionId' not in response:
254 response = response['value']
~/anaconda3/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py in execute(self, driver_command, params)
319 response = self.command_executor.execute(driver_command, params)
320 if response:
--> 321 self.error_handler.check_response(response)
322 response['value'] = self._unwrap_value(
323 response.get('value', None))
~/anaconda3/lib/python3.7/site-packages/selenium/webdriver/remote/errorhandler.py in check_response(self, response)
240 alert_text = value['alert'].get('text')
241 raise exception_class(message, screen, stacktrace, alert_text)
--> 242 raise exception_class(message, screen, stacktrace)
243
244 def _value_or_default(self, obj, key, default):
WebDriverException: Message: unknown error: Chrome failed to start: crashed
(unknown error: DevToolsActivePort file doesn't exist)
(The process started from chrome location /usr/bin/chromium-browser is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
因此,我阅读了许多消息,并从建议以前添加选项属性的人员那里获得了建议.因此,我将代码更改为以下代码:
so I read many messages and found advice from people who recommend to add options attributes before. So I change my code to this one:
from selenium.webdriver import Chrome
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-extensions')
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get('https://google.com')
但是我仍然没有启动任何浏览器窗口,并且我不明白发生了什么...而我的日志告诉了这个:
But I still don't have any browser windows launch and I can't understand what is happening ... and my log tells this :
/home/lclis/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:8: DeprecationWarning: use options instead of chrome_options
我正在寻找任何建议或建议,我有点迷茫,非常感谢您的帮助和时间阅读我的文章!
I'm looking for any advice or suggestion, I'm a little lost, thank you very much for your help and time to read my post !
PS:我在Windows10上,但是使用Ubuntu应用程序进行编码和使用Jupyter.
PS: I'm on windows10 but using Ubuntu app to code and use Jupyter.
推荐答案
这条第一条错误消息...
This first error message...
WebDriverException Traceback (most recent call last)
<ipython-input-16-dce6fb94cc37> in <module>
1 from selenium.webdriver import Chrome
2
----> 3 driver = Chrome()
~/anaconda3/lib/python3.7/site-packages/selenium/webdriver/chrome/webdriver.py in __init__(self, executable_path, port, options, service_args, desired_capabilities, service_log_path, chrome_options, keep_alive)
79 remote_server_addr=self.service.service_url,
80 keep_alive=keep_alive),
---> 81 desired_capabilities=desired_capabilities)
82 except Exception:
83 self.quit()
...表示 ChromeDriver 无法启动/产生新的浏览上下文,即 Chrome浏览器会话.
...implies that the ChromeDriver was unable to initiate/spawn a new Browsing Context i.e. Chrome Browser session.
理想情况下,您的代码块应该可以直接使用,但是当您将anaconda3
与ipython
结合使用时,您需要将 Key executable_path
与 Value 设置为 ChromeDriver 的绝对路径,如下所示:
Ideally your code block should have worked out of box, but as you are using anaconda3
with ipython
you need to pass the Key executable_path
along with the Value set to the absolute path of the ChromeDriver as follows:
from selenium.webdriver import Chrome
driver = Chrome(executable_path='/path/to/chromedriver')
但是,在您的第二次代码试用中,您非常接近.这第二条错误消息...
However, in your second code trial, you were pretty close. This second error message...
/home/lclis/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:8: DeprecationWarning: use options instead of chrome_options
...表示 ChromeDriver 再次无法启动/产生新的浏览上下文,即 Chrome浏览器会话.
...implies that the ChromeDriver was again unable to initiate/spawn a new Browsing Context i.e. Chrome Browser session.
您应该按以下方式使用options
,而不是chrome_options
:
Instead of chrome_options
you should have used options
as follows:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-extensions')
driver = webdriver.Chrome(options=chrome_options)
driver.get('https://google.com')
这篇关于为什么在Ubuntu 18.04上运行Selenium chrome驱动程序时无法启动chrome浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!