Selenium WebDriverException issue - python, cannot start browser both in Chrome/Firefox - python

I have the following script that makes use of selenium for authentication process.
I'm on Ubuntu 20.04 LTS, the computer was reformatted in the past few days, the same script worked before, from reading other threads I suspect that this has to do something with the root user/privileges.
I've tried both Firefox (geckodriver) and Chrome (chromedriver), here's what I did:
Chrome:
from selenium.webdriver import Chrome
from selenium.webdriver.chrome.options import Options
import chromedriver_autoinstaller
chromedriver_path = chromedriver_autoinstaller.install()
browser = Chrome(executable_path=chromedriver_path)
The error:
Traceback (most recent call last):
File "/home/or/Desktop/Tutorials/Selenium/chrome.py", line 7, in <module>
browser = Chrome(executable_path=chromedriver_path)
File "/home/or/anaconda3/lib/python3.8/site-packages/selenium/webdriver/chrome/webdriver.py", line 76, in __init__
RemoteWebDriver.__init__(
File "/home/or/anaconda3/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "/home/or/anaconda3/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/home/or/anaconda3/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/home/or/anaconda3/lib/python3.8/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: Chrome failed to start: exited abnormally.
(unknown error: DevToolsActivePort file doesn't exist)
(The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
Firefox:
from selenium.webdriver import Firefox
import geckodriver_autoinstaller
geckodriver_path = geckodriver_autoinstaller.install()
browser = Firefox(executable_path=geckodriver_path)
The error:
Traceback (most recent call last):
File "/home/or/Desktop/Tutorials/Selenium/firefox.py", line 7, in <module>
browser = Firefox(executable_path=geckodriver_path)
File "/home/or/anaconda3/lib/python3.8/site-packages/selenium/webdriver/firefox/webdriver.py", line 170, in __init__
RemoteWebDriver.__init__(
File "/home/or/anaconda3/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "/home/or/anaconda3/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/home/or/anaconda3/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/home/or/anaconda3/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: Process unexpectedly closed with status 1
Also in the geckodriver.log:
1620916609933 mozrunner::runner INFO Running command: "/usr/bin/firefox" "--marionette" "-foreground" "-no-remote" "-profile" "/tmp/rust_mozprofileVlJnBC"
Error: no DISPLAY environment variable specified
I've made sure that my chrome is installed in the default folder, tried to use options such as --no-sandbox, tried to set binary location to where chrome is, did not work.
I've installed chrome in its default path on Linux.
I've tried with the --no sandbox option
I've tried specified the binary path by setting `options.binary_path = '/path/to/google-chrome'
I've installed Xvfb on my machine
I've tried downloading the chromedriver and geckodriver manually and adding them to the path (after placing in /usr/local/...)

Related

Problems when running selenium with chromedriver_autoinstaller in Python

I have a following problem. I would like to run chromium on my Ubuntu using chromedriver_autoinstaller. My code is:
from selenium import webdriver
import chromedriver_autoinstaller
chromedriver_autoinstaller.install() # Check if the current version of chromedriver exists
# and if it doesn't exist, download it automatically,
# then add chromedriver to path
options = webdriver.ChromeOptions()
driver = webdriver.Chrome(options = options)
driver.get("http://www.python.org")
But desired url http://www.python.org does not open, I just see data:, in the browser:
I tried to add some options, but it did not help:
from selenium import webdriver
import chromedriver_autoinstaller
chromedriver_autoinstaller.install() # Check if the current version of chromedriver exists
# and if it doesn't exist, download it automatically,
# then add chromedriver to path
options = webdriver.ChromeOptions()
options.add_argument("--no-sandbox")
options.add_argument("--disable-dev-shm-usage")
options.add_argument('--remote-debugging-port=9222')
driver = webdriver.Chrome(options = options)
driver.get("http://www.python.org")
Full traceback:
Traceback (most recent call last):
File "/usr/lib/python3.8/code.py", line 90, in runcode
exec(code, self.locals)
File "<input>", line 14, in <module>
File "/home/vojtam/Desktop/greads_scrape/venv/lib/python3.8/site-packages/selenium/webdriver/chrome/webdriver.py", line 76, in __init__
RemoteWebDriver.__init__(
File "/home/vojtam/Desktop/greads_scrape/venv/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "/home/vojtam/Desktop/greads_scrape/venv/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/home/vojtam/Desktop/greads_scrape/venv/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/home/vojtam/Desktop/greads_scrape/venv/lib/python3.8/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: DevToolsActivePort file doesn't exist
Do you know, what is the problem here?
I found this questions, but it did not help me:
WebDriverException: unknown error: DevToolsActivePort file doesn't exist while trying to initiate Chrome Browser
unknown error: DevToolsActivePort file doesn't exist error while executing Selenium UI test cases on ubuntu
Tests fail immediately with unknown error: DevToolsActivePort file doesn't exist when running Selenium grid through systemd
The problem was that Docker requires chrome_options.add_argument("--headless") when running chrome in selenium

why doesn't selenium work properly on manjaro (PATH problem)

i just downloaded selenium and wanted to try just simple
code:
from selenium import webdriver
driver = webdriver.Firefox(executable_path='/usr/local/bin/geckodriver')
driver.get("https://www.youtube.com")
error:
Traceback (most recent call last):
File "/home/keksik/JetBainz/PycharmProjects/pythonProject1/main.py", line 3, in <module>
driver = webdriver.Firefox(executable_path='/usr/local/bin/geckodriver')
File "/home/keksik/JetBainz/PycharmProjects/pythonProject1/venv/lib/python3.8/site-packages/selenium/webdriver/firefox/webdriver.py", line 170, in __init__
RemoteWebDriver.__init__(
File "/home/keksik/JetBainz/PycharmProjects/pythonProject1/venv/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "/home/keksik/JetBainz/PycharmProjects/pythonProject1/venv/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/home/keksik/JetBainz/PycharmProjects/pythonProject1/venv/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/home/keksik/JetBainz/PycharmProjects/pythonProject1/venv/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.SessionNotCreatedException: Message: Unable to find a matching set of capabilities
it also doesn't work if i change
driver = webdriver.Firefox(executable_path='/usr/local/bin/geckodriver')
to
driver = webdriver.Firefox()
i made gecko driver executable with chmod +x geckodriver
moved it to usr/local/bin
and set path like that:
export PATH=$PATH:/usr/local/bin/geckodriver
still doesn't work
with webdriver-manager the error is
Traceback (most recent call last):
File "/home/keksik/JetBainz/PycharmProjects/pythonProject1/main.py", line 4, in <module>
driver = webdriver.Firefox(executable_path=GeckoDriverManager().install())
File "/home/keksik/JetBainz/PycharmProjects/pythonProject1/venv/lib/python3.8/site-packages/selenium/webdriver/firefox/webdriver.py", line 170, in __init__
RemoteWebDriver.__init__(
File "/home/keksik/JetBainz/PycharmProjects/pythonProject1/venv/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "/home/keksik/JetBainz/PycharmProjects/pythonProject1/venv/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/home/keksik/JetBainz/PycharmProjects/pythonProject1/venv/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/home/keksik/JetBainz/PycharmProjects/pythonProject1/venv/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.SessionNotCreatedException: Message: Unable to find a matching set of capabilities
Appearently, the issue occurs due to non-administrative privileges. The location where you have your driver at needs root privileges to be executed/run. I suggest you moving your driver to somewhere in your home directory where you don't need root privileges to launch the driver.
You can always install the Firefox driver on the fly. Just make sure you have the normal version of firefox installed. This doesn't have any kind of dependency on your local setup.
All you need to do is:
self.driver = webdriver.Firefox(executable_path=GeckoDriverManager().install())
from
from webdriver_manager.firefox import GeckoDriverManager

Can't run selenium script remotely

I have a python selenium script. I want to run the script on remotely connected(using ssh) PC. when i run the script directly on that PC, it run. but, while i run the same script remotely, it throw error.
Traceback (most recent call last):
File "crawling_script.py", line 14, in <module>
driver = webdriver.Chrome(executable_path='/var/www/html/hariharan/health_grades/chromedriver')
File "/var/www/html/hariharan/health_grades/env/local/lib/python2.7/site-packages/selenium/webdriver/chrome/webdriver.py", line 81, in __init__
desired_capabilities=desired_capabilities)
File "/var/www/html/hariharan/health_grades/env/local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "/var/www/html/hariharan/health_grades/env/local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/var/www/html/hariharan/health_grades/env/local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/var/www/html/hariharan/health_grades/env/local/lib/python2.7/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: Chrome failed to start: exited abnormally
(unknown error: DevToolsActivePort file doesn't exist)
(The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
(Driver info: chromedriver=73.0.3683.68 (47787ec04b6e38e22703e856e101e840b65afe72),platform=Linux 4.4.0-145-generic x86_64)
Please give me a solution to run the selenium python script remotely.
Thanks in advance.
Try ssh -X, it forwards graphical output to local machine.

Selenium fails to start Chromedriver - Ubuntu 16.04, Python 3.6.3

from selenium import webdriver
browser = webdriver.Chrome()
browser.get('http://www.google.com')
It never actually reaches this and fails with:
root#server:~# python /root/EmailBot/main.py
Traceback (most recent call last):
File "/root/EmailBot/main.py", line 22, in <module>
browser = webdriver.Chrome()
File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/chrome/webdriver.py", line 69, in __init__
desired_capabilities=desired_capabilities)
File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webdriver.py", line 151, in __init__
self.start_session(desired_capabilities, browser_profile)
File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webdriver.py", line 240, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webdriver.py", line 308, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally
(Driver info: chromedriver=2.33.506092 (733a02544d189eeb751fe0d7ddca79a0ee28cce4),platform=Linux 4.4.0-101-generic x86_64)
of importance:
root#server:~# which chromedriver
/usr/local/bin/chromedriver
root#server:~# ll /usr/local/bin/chromedriver
-rwxr-xr-x 1 root root 8950080 Oct 3 14:09 /usr/local/bin/chromedriver*
root#server:~# google-chrome --version
Google Chrome 62.0.3202.94
I have tried EVERY thread I can find and no solutions are working. I'm not sure if this is because it is a python 3.6 problem?
Did you try giving the chrome webdriver path directly inside the parenthesis?
Same code works perfectly on my windows machine.
I have set environment variables, else i need to provide the path directly.

Selenium Firefox 56 webdriver via geckodriver results in error: Can not connect to the Service

I am using geckodriver for Firefox and I'm running Python 3 with Selenium. This is my code in the file script.py:
from selenium import webdriver
browser = webdriver.Firefox()
browser.get('http://www.google.com')
Once I run it with python3 script.py with my terminal, it launches Firefox but simply doesn't load the page, nor does it insert the link in the address bar. My geckodriver is installed at this location: /usr/local/bin/geckodriver.
After waiting for about a minute, I get the following error:
Traceback (most recent call last):
File "script.py", line 3, in <module>
browser = webdriver.Firefox()
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/firefox/webdriver.py", line 154, in __init__
keep_alive=True)
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/remote/webdriver.py", line 151, in __init__
self.start_session(desired_capabilities, browser_profile)
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/remote/webdriver.py", line 240, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/remote/webdriver.py", line 308, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: connection refused
I already searched a lot for fixes but none of them worked.
After I realized, I was using Firefox ESR instead of the normal one, I switched to the normal Firefox version and faced this problem:
Traceback (most recent call last):
File "script.py", line 3, in <module>
driver = webdriver.Firefox(executable_path=r'/root/Downloads/firefox-56.0.1/firefox/firefox')
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/firefox/webdriver.py", line 144, in __init__
self.service.start()
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/common/service.py", line 102, in start
raise WebDriverException("Can not connect to the Service %s" % self.path)
selenium.common.exceptions.WebDriverException: Message: Can not connect to the Service /root/Downloads/firefox-56.0.1/firefox/firefox
Which version of Selenium/Geckodriver/Browser are you using?
Anyway, do you set the executable_path:
driver = webdriver.Firefox(executable_path=r'pathTo/geckodriver')
?
EDIT
If you are using:
Selenium version = 3.6.0
Geckodriver version = 0.19.0
Firefox version = 52.4.0
Your problem is Firefox. From GeckoDriver releases, is recommended Firefox 55.0 (and greater).

Categories