Why chrome browser doesn't launch when running Selenium chrome driver on Ubuntu 18.04

China☆狼群 提交于 2020-02-05 02:50:10

问题


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() 

I got this 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()

~/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: I'm on windows10 but using Ubuntu app to code and use Jupyter.


回答1:


If I am not mistaken, since you added the headless option, the browser won't open any window.

Try to remove this line: chrome_options.add_argument('--headless')




回答2:


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()

...implies that the ChromeDriver was unable to initiate/spawn a new Browsing Context i.e. Chrome Browser session.

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

...implies that the ChromeDriver was again unable to initiate/spawn a new Browsing Context i.e. Chrome Browser session.

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')


来源:https://stackoverflow.com/questions/59989038/why-chrome-browser-doesnt-launch-when-running-selenium-chrome-driver-on-ubuntu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!