Running Selenium WebDriver Python bindings in Chrome
Asked Answered
F

9

154

I ran into a problem while working with Selenium. For my project, I have to use Chrome. However, I can't connect to that browser after launching it with Selenium.

For some reason, Selenium can't find Chrome by itself. This is what happens when I try to launch Chrome without including a path:

Traceback (most recent call last):
  File "./obp_pb_get_csv.py", line 73, in <module>
    browser = webdriver.Chrome() # Get local session of chrome
  File "/usr/lib64/python2.7/site-packages/selenium/webdriver/chrome/webdriver.py", line 46, in __init__
    self.service.start()
  File "/usr/lib64/python2.7/site-packages/selenium/webdriver/chrome/service.py", line 58, in start
    and read up at http://code.google.com/p/selenium/wiki/ChromeDriver")
selenium.common.exceptions.WebDriverException: Message: 'ChromeDriver executable needs to be available in the path.                 Please download from http://code.google.com/p/selenium/downloads/list                and read up at http://code.google.com/p/selenium/wiki/ChromeDriver'

To solve this problem, I then included the Chromium path in the code that launches Chrome. However, the interpreter fails to find a socket to connect to:

Traceback (most recent call last):
  File "./obp_pb_get_csv.py", line 73, in <module>
    browser = webdriver.Chrome('/usr/bin/chromium') # Get local session of chrome
  File "/usr/lib64/python2.7/site-packages/selenium/webdriver/chrome/webdriver.py", line 46, in __init__
    self.service.start()
  File "/usr/lib64/python2.7/site-packages/selenium/webdriver/chrome/service.py", line 64, in start
    raise WebDriverException("Can not connect to the ChromeDriver")
selenium.common.exceptions.WebDriverException: Message: 'Can not connect to the ChromeDriver'

I also tried solving the problem by launching chrome with:

chromium --remote-shell-port=9222

However, this did not work either.

PS. Here's some information about my system:

www-client: chromium 15.0.874.121
dev-lang:   python 2.7.2-r3 Selenium 2.11.1
OS:         GNU/Linux Gentoo Kernel 3.1.0-gentoo-r1
Fredenburg answered 24/11, 2011 at 10:57 Comment(1)
I just wanted to let you know that "--remote-shell-port" is not a valid command line option. See: peter.sh/experiments/chromium-command-line-switches ("--remote-debugging-port" would be the correct one. Please be aware that you need to close all chromium instances before using that option. I.e. creating a new window in an existing browser session does not work).Opine
I
137

You need to make sure the standalone ChromeDriver binary (which is different than the Chrome browser binary) is either in your path or available in the webdriver.chrome.driver environment variable.

see http://code.google.com/p/selenium/wiki/ChromeDriver for full information on how wire things up.

Edit:

Right, seems to be a bug in the Python bindings wrt reading the chromedriver binary from the path or the environment variable. Seems if chromedriver is not in your path you have to pass it in as an argument to the constructor.

import os
from selenium import webdriver

chromedriver = "/Users/adam/Downloads/chromedriver"
os.environ["webdriver.chrome.driver"] = chromedriver
driver = webdriver.Chrome(chromedriver)
driver.get("http://stackoverflow.com")
driver.quit()
Ideal answered 24/11, 2011 at 15:7 Comment(3)
Sorry forget to mention that. I got this link.Fredenburg
Tried the Chromedriver instead of Firefox Webdriver.. almost 3 times performance gain ..Sputnik
Also, just want to mention that the argument passed to the constructor is the path to ChromeDriver binary and not the directory containing the ChromeDriver binary (wasted the past half an hour figuring out what was going wrong).Karyolymph
S
115

For Linux

  1. Check you have installed latest version of chrome brwoser-> chromium-browser -version

  2. If not, install latest version of chrome sudo apt-get install chromium-browser

  3. get appropriate version of chrome driver from here

  4. Unzip the chromedriver.zip

  5. Move the file to /usr/bin directory sudo mv chromedriver /usr/bin

  6. Goto /usr/bin directory cd /usr/bin

  7. Now, you would need to run something like sudo chmod a+x chromedriver to mark it executable.

  8. finally you can execute the code.

    from selenium import webdriver
    
    driver = webdriver.Chrome()
    driver.get("http://www.google.com")
    print driver.page_source.encode('utf-8')
    driver.quit()
    
Shortage answered 23/6, 2014 at 11:4 Comment(5)
Very nice! One note: I was only able to get this to work with the 32-bit chromedriver although I am running 64-bit linux.Blackfish
On Ubuntu (14.04) you can install chromium-chromdriver package (e.g. with apt-get) and add the path of the chromedriver file to the PATH shell variable PATH="${PATH}":/usr/lib/chromium-browser/ If you add this to your .bashrc, you won't need to set it each time you want to test with selenium.Inerney
actually this answer should have a lot more upvotes. basically this is the way to go for every driver like the geckodriver.Unsocial
My chromium-browser version is mentioned as (Chromium 67.0.3396.99 Built on Ubuntu , running on Ubuntu 16.04) but the link only has versions as 2.0-2.9. Need some help.Cullie
Please remove the display.stop() @Vicky, that doesn't work and gives ab error. By the way, thanks. May God bless you.Hebraist
V
94

Mac OS X only

An easier way to get going (assuming you already have Homebrew installed, which you should, if not, go do that first and let Homebrew make your life better) is to just run the following command:

brew install chromedriver

That should put the chromedriver in your path and you should be all set.

Voiced answered 20/1, 2012 at 19:30 Comment(4)
It can't work on Mac. selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH.Hy
@Hy Your environment might not be setup right. Try running brew doctor and see if that finds anything.Voiced
This is one of those warm and fuzzy moments when you're just happy that you have a Mac. So easy.Initiatory
HEART! I love brewEqui
H
54

For Windows

Download ChromeDriver from this direct link or get the latest version from this page.

Paste the chromedriver.exe file in your C:\Python27\Scripts folder.

This should work now:

from selenium import webdriver
driver = webdriver.Chrome()
Harar answered 23/9, 2014 at 6:16 Comment(0)
Z
23

For Windows, please have the chromedriver.exe placed under <Install Dir>/Python27/Scripts/.

Zee answered 7/4, 2013 at 10:54 Comment(3)
and where to put in ubuntu ? int /usr/lib/ or somewhere else?Karbala
Having the file in PATH wasn't enough, putting it to /Scripts/ where the ride.py is worked.Mannerheim
Please suggest me in Linux envi? Thanks!Jeraldjeraldine
O
1

There are two ways to run Selenium Python tests in Google Chrome. I'm considering Windows (Windows 10 in my case):

Prerequisite: Download the latest Chrome Driver from: Downloads - ChromeDriver - WebDriver for Chrome

Way 1:

i) Extract the downloaded ZIP file in a directory/location of your choice
ii) Set the executable path in your code as below:

self.driver = webdriver.Chrome(executable_path='D:\Selenium_RiponAlWasim\Drivers\chromedriver_win32\chromedriver.exe')

Way 2:

i) Simply paste the chromedriver.exe under <Install Dir>/Python/Scripts/ (In my case, the folder was: C:\Python36\Scripts)
ii) Now write the simple code as below:

self.driver = webdriver.Chrome()
Ogbomosho answered 24/7, 2017 at 12:47 Comment(1)
The download site has "Please note that we have migrated to a new ChromeDriver site. Current site will be deprecated soon."Wellborn
E
1

On Ubuntu, you can just do this:

sudo apt install chromium-chromedriver

and it should work.

Ene answered 27/2, 2022 at 21:23 Comment(0)
T
0

For Windows' IDE:

If your path doesn't work, you can try to add the chromedriver.exe to your project, like in this project structure.

chromedriver.exe

Then you should load the chromedriver.exe in your main file. As for me, I loaded the driver.exe in driver.py.

def get_chrome_driver():
return webdriver.Chrome("..\\content\\engine\\chromedriver.exe",
                            chrome_options='--no-startup-window')

.. means driver.py's upper directory

. means the directory where the driver.py is located

Terzetto answered 10/6, 2017 at 5:22 Comment(0)
N
0

Since Selenium 4.6.0, you don't need to manually install Selenium Manager(webdriver-manager) as shown below because it is already included in Selenium according to the blog

pip install webdriver-manager

And, since Selenium 4.11.0, the code below is enough because Selenium Manager can automatically discover your browser version installed in your machine, then can automatically download the proper driver version for it according to the blog:

from selenium import webdriver

driver = webdriver.Chrome()

*My answer explains it with how to test Django Admin with Chrome, Selenium, pytest-django and Django.

Nomenclator answered 4/9, 2023 at 17:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.