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:
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
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.
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()
Mac OSX 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.
- Check you have installed latest version of chrome brwoser->
- If not, install latest version of chrome
sudo apt-get install chromium-browser
- get appropriate version of chrome driver from here
- Unzip the chromedriver.zip
- Move the file to
sudo mv chromedriver /usr/bin
- Now, you would need to run something like
sudo chmod a+x chromedriverto mark it executable.
finally you can execute the code.
import os from selenium import webdriver from pyvirtualdisplay import Display display = Display(visible=0, size=(800, 600)) display.start() driver = webdriver.Chrome() driver.get("http://www.google.com") print driver.page_source.encode('utf-8') driver.quit() display.stop()
Download webdriver from:
download the latest chromedriver from here
Paste the chromedriver.exe file in “C:\Python27\Scripts” Folder.
This should work now.
from selenium import webdriver driver = webdriver.Chrome()
For windows, please have the
chromedriver.exe placed under
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.
Then you should load the
chromedriver.exe in your main file. As for me, I loaded the
def get_chrome_driver(): return webdriver.Chrome("..\content\engine\chromedriver.exe", chrome_options='--no-startup-window')
driver.py's upper directory
. means the directory where the
driver.py is located
Hope this will be helpful.
There are 2 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: https://sites.google.com/a/chromium.org/chromedriver/downloads
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')
i) Simply paste the chromedriver.exe under /Python/Scripts/ (In my case the folder was: C:\Python36\Scripts)
ii) Now write the simple code as below:
self.driver = webdriver.Chrome()