How to launch Chrome in Selenium correctly - python

I can not launch Chrome in Selenium.
driver=webdriver.Chrome()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/anaconda/lib/python2.7/site-packages/selenium/webdriver/chrome/webdriver.py", line 67, in __init__
desired_capabilities=desired_capabilities)
File "/usr/anaconda/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 87, in __init__
self.start_session(desired_capabilities, browser_profile)
File "/usr/anaconda/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 141, in start_session
'desiredCapabilities': desired_capabilities,
File "/usr/anaconda/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 201, in execute
self.error_handler.check_response(response)
File "/usr/anaconda/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 181, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: crashed
(Driver info: chromedriver=2.33.506092 (733a02544d189eeb751fe0d7ddca79a0ee28cce4),platform=Linux 3.19.8-100.fc20.x86_64 x86_64)
I use "chromedriver_linux64.zip 2017-10-03 21:09:52 3.90MB" from
url.

You are using the current latest chromedriver 2.33 with Google Chrome 38.0.2125.104.
From the release notes, the support for this version is:
----------ChromeDriver v2.33 (2017-10-03)----------
Supports Chrome v60-62
Make sure also that you are using the latest stable version of selenium.
Furthermore, from Help WebDriver find the downloaded ChromeDriver executable, you should do one of these
include the ChromeDriver location in your PATH environment variable
(Python only) include the path to ChromeDriver when instantiating webdriver.Chrome (see sample below)

If you're going to use an old version of Chrome - you will need to match the version of chromedriver to it. In your case you are using Chrome 38, which was last officially supported by ChromeDriver 2.13, which you can find here
If your users are using newer versions of Chrome, I would recommend updating your distro and installed chrome to match them.

The error says it all :
File "<stdin>", line 1, in <module>
Seems the error occurs on the very first line which is as :
driver=webdriver.Chrome()
This is because here in this code block unless you import webdriver, the driver object is unable to properly initiate/handle the instance of WebBrowser i.e. Chrome Browser.
Solution :
While working with Selenium 3.x, ChromeDriver 2.33.x you need Chrome Browser v60-62 with Python 3.x bindings, and you have to do the following:
Download the chromedriver binary from this link.
Import the necessary modules
Provide the absolute path of the chromedriver binary in your system.
Below is the minimal code :
//The Linux Example
from selenium import webdriver
driver = webdriver.Chrome(executable_path=r'/usr/bin/chromedriver')
driver.get('https://www.google.co.in')
print("Page Title is : %s" %driver.title)
driver.quit()
//The Windows Example
from selenium import webdriver
driver = webdriver.Chrome(executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
driver.get('https://www.google.co.in')
print("Page Title is : %s" %driver.title)
driver.quit()
Update :
On a separate note as you are seeing WebDriverException: Message: unknown error: Chrome failed to start: crashed perform the following additional steps :
Uninstall Google Chrome from your system through Revo Uninstaller.
Run CCleaner to wipe out all the unwanted OS chores.
Take a system Reboot
Install latest official build of Google Chrome
Execute your Test.

import the selenium and chrome.option to let chrom run in headlessmode
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
if you use chrome or chromiun as root user on linux you should add "--no-sandbox" option and set windows size to avoid some item no show because the window size is too small
chrome_options = Options()
chrome_options.add_argument("--headless") # headless mode
chrome_options.add_argument("--no-sandbox") # run as root user should add --no-sandbox option
chrome_options.add_argument("--window-size=1920x1080")
you can download the chrome driver from http://chromedriver.storage.googleapis.com the last version is 2.38
chrome_driver = "path to chromedriver"
# start the driver
driver = webdriver.Chrome(chrome_options=chrome_options, executable_path=chrome_driver)
driver.get("https://www.google.com")

Related

selenium.common.exceptions.SessionNotCreatedException: Message: session not created: This version of ChromeDriver only supports Chrome version 83

So this is my first time working with selenium, and I get the chromedriver browser to open fo a quarter of a second and then it closes, even it I give it a webpage to load or I tell it to sleep. It also gives me a SessionNotCreatedException.
Code:
from selenium import webdriver
import time
class TestBot():
def __init__(self):
self.driver = webdriver.Chrome()
self.driver.get('http://www.google.com/')
time.sleep(5)
Error:
>>> bot = TestBot()
DevTools listening on ws://127.0.0.1:55101/devtools/browser/7029bc33-860f-42b8-8fb5-751cf27f82bc
[16708:17644:0524/045808.952:ERROR:browser_switcher_service.cc(238)] XXX Init()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "test_bot.py", line 6, in __init__
self.driver = webdriver.Chrome()
File "C:\Users\Caleb\test_bot\test_bot\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 81, in __init__
desired_capabilities=desired_capabilities)
File "C:\Users\Caleb\test_bot\test_bot\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "C:\Users\Caleb\test_bot\test_bot\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "C:\Users\Caleb\test_bot\test_bot\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\Caleb\test_bot\test_bot\lib\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: This version of ChromeDriver only supports Chrome version 83
I've tried adding Chromedriver as a path variable and that didn't work. I can start a chromedriver session just fine in cmd.
I think you should try running Selenium with MozillaFirefox,you also will need to install Geckodriver, which is a software that acts as a bridge between Firefox and selenium.
I was looking at the wrong version of chrome, turns out I have version 81, so I installed webdriver-manager and that works
as we see in the last line:
selenium.common.exceptions.SessionNotCreatedException: Message: session not created: This version of ChromeDriver only supports Chrome version 83
try to:
update Chrome to last version so selenium too
use geckodriver.exe for chrome , and put inside a folder then try:
self.driver = webdriver.Chrome(r"c:\your\geckodriver\path\withput\")
This error message...
selenium.common.exceptions.SessionNotCreatedException: Message: session not created: This version of ChromeDriver only supports Chrome version 83
...implies that the ChromeDriver was unable to initiate/spawn a new Browsing Context i.e. Chrome Browser session.
Analysis
Your main issue is the incompatibility between the version of the binaries you are using as follows:
You are using ChromeDriver 83.0
Release Notes of ChromeDriver 83.0 clearly mentions the following :
Supports Chrome version 83
Most possibly the Chrome Browser haven't been updated to Chrome v83.0 yet as a part of the recent push and still is Chrome v81.0
Your Selenium Client version is unknown to us.
So there is a clear mismatch between ChromeDriver v83.0 and the Chrome Browser v81.0
Solution
Ensure that:
Selenium is upgraded to current levels Version 3.141.59.
ChromeDriver is updated to current ChromeDriver v83.0 level.
Chrome is updated to current Chrome Version 83.0 level. (as per ChromeDriver v83.0 release notes)
If your base Web Client version is too old, then uninstall it and install a recent GA and released version of Web Client.

This version of ChromeDriver only supports Chrome version 79 - Python

This error drives me crazy. My code is:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import chromedriver_binary
from selenium.webdriver.common.by import By
import time
from influxdb import InfluxDBClient
chrome_options = Options()
chrome_options.add_argument("--headless")
chromedriver_binary =
"/home/dario/scripts/cron_run/web_app_login_checker/chromedriver/chromedriver"
driver = webdriver.Chrome(chromedriver_binary, options=chrome_options)
If I do:
./chromedriver -v
ChromeDriver 79.0.3945.36 (3582db32b33893869b8c1339e8f4d9ed1816f143-refs/branch-
heads/3945#{#614})
It's the right version.
Full traceback is:
Traceback (most recent call last):
File "grafana.py", line 12, in <module>
driver = webdriver.Chrome(chromedriver_binary, options=chrome_options)
File
"/home/dario/scripts/cron_run/web_app_login_checker/lib/python3.6/site-
packages/selenium/webdriver/chrome/webdriver.py", line 81, in __init__
desired_capabilities=desired_capabilities)
File
"/home/dario/scripts/cron_run/web_app_login_checker/lib/python3.6/site-
packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File
"/home/dario/scripts/cron_run/web_app_login_checker/lib/python3.6/site-
packages/selenium/webdriver/remote/webdriver.py", line 252, in
start_session
response = self.execute(Command.NEW_SESSION, parameters)
File
"/home/dario/scripts/cron_run/web_app_login_checker/lib/python3.6/site-
packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File
"/home/dario/scripts/cron_run/web_app_login_checker/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: This version of ChromeDriver only supports Chrome version
79
None of the already discussed topics here on Stack Overflow helped me.
The problem is the Chrome browser version, not the ChromeDriver version. You need to update it to version 79, or downgrade the ChromeDriver. You can find here the matching versions.
If you are getting this error when you run stuffs on automated cluster and you are downloading the stable version of the google chrome every time then you can use the below shell script to download the compatible version of the chrome driver dynamically every time even if the stable version of the chrome gets updated. You could do something similar to this python to make it work.
%sh
#downloading compatible chrome driver version
#getting the current chrome browser version
**chromeVersion=$(google-chrome --product-version)**
#getting the major version value from the full version
**chromeMajorVersion=${chromeVersion%%.*}**
# setting the base url for getting the release url for the chrome driver
**baseDriverLatestReleaseURL=https://chromedriver.storage.googleapis.com/LATEST_RELEASE_**
#creating the latest release driver url based on the major version of the chrome
**latestDriverReleaseURL=$baseDriverLatestReleaseURL$chromeMajorVersion**
**echo $latestDriverReleaseURL**
#file name of the file that gets downloaded which would contain the full version of the chrome driver to download
**latestDriverVersionFileName="LATEST_RELEASE_"$chromeMajorVersion**
#downloading the file that would contain the full release version compatible with the major release of the chrome browser version
**wget $latestDriverReleaseURL**
#reading the file to get the version of the chrome driver that we should download
**latestFullDriverVersion=$(cat $latestDriverVersionFileName)**
**echo $latestFullDriverVersion**
#creating the final URL by passing the compatible version of the chrome driver that we should download
**finalURL="https://chromedriver.storage.googleapis.com/"$latestFullDriverVersion"/chromedriver_linux64.zip"**
**echo $finalURL**
**wget $finalURL**
I was able to get the compatible version of chrome browser and chrome driver using the above approach when running scheduled job on the databricks environment and it worked like a charm without any issues.
Hope it helps others in one way or other.
Check your chrome version
Download the same version of the chrome driver
Check the latest driver here
Update your driver in the reference folder.
In my case, I am using the windows 10 and python I've updated with the latest chromedriver.exe file (for Chrome v83)
This error message...
selenium.common.exceptions.SessionNotCreatedException: Message: session not created: This version of ChromeDriver only supports Chrome version 79
...implies that the ChromeDriver was unable to initiate/spawn a new Browsing Context i.e. Chrome Browser v79.x session.
Your main issue is the incompatibility between the version of the binaries you are using as follows:
You are using chromedriver=79.0.3945.36
Release Notes of chromedriver=79.0.3945.36 clearly mentions the following :
Supports Chrome version 79
Presumably you are using the latest Version 78.0.3904.108 (Official Build)
So there is a clear mismatch between the ChromeDriver v79.0.3945.36 and Chrome Browser v78.0.3904.108
Solution
There are two possible solutions:
Either you need to downgrade ChromeDriver to ChromeDriver 78.0.3904.105
Download, install and use Chrome from the Chrome Release Channels
Linux Stable channel
Linux Beta channel
Linux Development channel
latest build

How to setup Selenium with headless Firefox on Raspbian Buster

I have gotten this to work in Jessie/Stretch with previous Firefox versions but can't seem to get it to work with Raspberry Pi 3B or 4 running Raspbian Buster and the latest Selenium, 3.141.0 in Python.
Since iceweasel is no longer available I install firefox-esr via apt and get the latest ARM geckodriver, which is currently 0.23.0, from https://github.com/mozilla/geckodriver/releases then run the code below:
from selenium import webdriver
from pyvirtualdisplay import Display
from selenium.webdriver.firefox.options import Options
options = Options()
options.headless = True
capabilities = webdriver.DesiredCapabilities().FIREFOX
capabilities["marionette"] = False
driver = webdriver.Firefox(options=options, executable_path='/usr/bin/geckodriver', capabilities=capabilities)
driver.get('https://www.google.com')
print(driver.page_source)
driver.close()
display.stop()
This returns the error:
Message: Can't load the profile. Possible firefox version mismatch. You must use GeckoDriver instead for Firefox 48+. Profile Dir: /tmp/tmpxMUUcF If you specified a log_file in the FirefoxBinary constructor, check it for details.
Traceback (most recent call last):
File "SeleniumDebugExample.py", line 50, in <module>
driver = webdriver.Firefox(options=options, executable_path='/srv/main/geckodriver', capabilities=capabilities)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/webdriver.py", line 191, in __init__
self.binary, timeout)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/extension_connection.py", line 52, in __init__
self.binary.launch_browser(self.profile, timeout=timeout)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/firefox_binary.py", line 73, in launch_browser
self._wait_until_connectable(timeout=timeout)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/firefox_binary.py", line 114, in _wait_until_connectable
% (self.profile.path))
WebDriverException: Message: Can't load the profile. Possible firefox version mismatch. You must use GeckoDriver instead for Firefox 48+. Profile Dir: /tmp/tmpxMUUcF If you specified a log_file in the FirefoxBinary constructor, check it for details.
I am specifying geckodriver though so I am not sure what the issue is...
If anyone has gotten headless selenium to work on Raspbian Buster and can share some could it would be greatly appreciated!
After a bunch of testing it seems the issue was with adding the marrionette=False capability... Back in Jessie with iceweasel I had needed to add that for it to work but it seems with firefox-esr that is no longer necessary... I was able to get this working with just the below:
from selenium import webdriver
from pyvirtualdisplay import Display
driver = webdriver.Firefox(executable_path='/usr/bin/geckodriver')
driver.get('https://www.google.com')
print(driver.page_source)
driver.close()
display.stop()

Selenium doesn't open the specified URL and shows data:,

I am trying to open the URL using selenium in chrome. I have chromedriver available with me.
following is the code I want to execute.
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--disable-infobars")
driver = webdriver.Chrome(executable_path="./chromedriver", chrome_options=chrome_options)
driver.get("https://google.com")
The browser is opened successfully but it doesn't open the specified URL. The URL in the browser is data:,.
Any help will be greatly appreciated. Please!
Please see the attached image.
Note: Selenium version : 3.14.0
I get the following error on closing the chrome tab.
File "test.py", line 6, in <module>
driver = webdriver.Chrome(executable_path="./chromedriver", chrome_options=chrome_options)
File "/home/speedious/anaconda3/lib/python3.5/site-packages/selenium/webdriver/chrome/webdriver.py", line 75, in __init__
desired_capabilities=desired_capabilities)
File "/home/speedious/anaconda3/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 156, in __init__
self.start_session(capabilities, browser_profile)
File "/home/speedious/anaconda3/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 251, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/home/speedious/anaconda3/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 320, in execute
self.error_handler.check_response(response)
File "/home/speedious/anaconda3/lib/python3.5/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 normally
(chrome not reachable)
(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=2.42.591071 (0b695ff80972cc1a65a5cd643186d2ae582cd4ac),platform=Linux 4.10.0-37-generic x86_64)
This error message...
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited normally
(chrome not reachable)
(The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
...implies that the ChromeDriver instance was unable to start the Chrome Browser process.
Your main issue is the google-chrome is no longer present at the expected default location of /usr/bin/
As per ChromeDriver - Requirements the server expects you to have Chrome installed in the default location for each system:
1 For Linux systems, the ChromeDriver expects /usr/bin/google-chrome to be a symlink to the actual Chrome binary. You can also override the Chrome binary location as follows:
A Windows OS based example:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument("start-maximized")
options.binary_location("C:\\path\\to\\chrome.exe")
driver = webdriver.Chrome(executable_path=r'C:\path\to\chromedriver.exe', chrome_options=options)
driver.get('http://google.com/')
Additional Considerations
Upgrade ChromeDriver to current ChromeDriver v2.42 level.
Keep Chrome version between Chrome v68-70 levels. (as per ChromeDriver v2.42 release notes)
Clean your Project Workspace through your IDE and Rebuild your project with required dependencies only.
If your base Web Client version is too old, then uninstall it through Revo Uninstaller and install a recent GA and released version of Web Client.
Execute your #Test.
I found another reason for this behavior, which I don't see listed here. I believe Selenium libraries require Chrome Developer Tools to not be disabled. I was running into this issue and getting this regkey set got me past it.
Key - HKLM\Software\Policies\Google\Chrome
DWORD - DeveloperToolsDisabled
Value - 0

chrome driver is not working in windows

I want to run my python selenium scripts in chrome browser on windows. I downloaded chrome driver and placed the exe file in "C:\Python27\Scripts" but still I'm getting error as
Traceback (most recent call last):
File "C:\Users\INNOVATE\Desktop\workspace\Sample\sample.py", line 3, in <module>
driver = webdriver.Chrome()
File "C:\Python27\lib\site-packages\selenium-2.44.0-py2.7.egg\selenium\webdriver\chrome\webdriver.py", line 59, in __init__
self.service.start()
File "C:\Python27\lib\site-packages\selenium-2.44.0-py2.7.egg\selenium\webdriver\chrome\service.py", line 66, in start
"ChromeDriver executable needs to be available in the path. "
selenium.common.exceptions.WebDriverException: Message: ChromeDriver executable needs to be available in the path. Please download from http://chromedriver.storage.googleapis.com/index.html and read up at http://code.google.com/p/selenium/wiki/ChromeDriver
what should I do?
try this package.
from selenium import webdriver
import chromedriver_autoinstaller
chromedriver_autoinstaller.install()
#maximize the chrome
chrome_options = Options()
chrome_options.add_argument("--start-maximized")
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get("https://www.google.com")
you can defiantly remove chrome_options or add more arguments.
Please see the documentation, https://pypi.org/project/chromedriver-autoinstaller/
Please make sure you have performed all the required steps to open Chrome using Selenium.
Step1: Download Chrome Driver
Latest Release of Chrome Driver you can found is ChromeDriver 2.13.
Please use Link Chrome Driver to get the latest one.
Step2: Save the Chrome Driver to your Project Location and give the path of that folder in your code.
In my case i have saved the Chrome Driver in 'S' drive.
System.setProperty("webdriver.chrome.driver","S://Chrome Driver//chromedriver.exe");
Please check the path again where i believe is the problem in your code.

Categories