0

I am trying to use chromedriver with Selenium on Windows 10 but I get the following error:

Traceback (most recent call last):
  File "scrape.py", line 4, in <module>
    driver = webdriver.Chrome()
  File "C:\Python37\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 73, in __init__
    self.service.start()
  File "C:\Python37\lib\site-packages\selenium\webdriver\common\service.py", line 83, in start
    os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home

And here's my test script:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome()
driver.get("http://www.python.org")
assert "Python" in driver.title
elem = driver.find_element_by_name("q")
elem.clear()
elem.send_keys("pycon")
elem.send_keys(Keys.RETURN)
assert "No results found." not in driver.page_source
driver.close()

I've tried many things. I'll detail below.

I've attempted to add the path to chromedriver to PATH. Image here:

enter image description here

This works fine because I can run chromedriver from the commandline:

C:\Users\KraftWurk>chromedriver
Starting ChromeDriver 74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729@{#29}) on port 9515
Only local connections are allowed.
Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.

But still, when I run my scripts I get the error that chromedriver needs to be in PATH ... it is, so not sure what's going on there.

I've read the following: Python Selenium Chrome Webdriver

I've attempted to provide the path as suggested using:

driver = webdriver.Chrome(executable_path=r"C:\drivers\chromedriver.exe")

I still get the same warning.

I'm not quite sure what's going on. I'm using Python 3.7 on Windows 10. Selenium 3.141.0 and Chromedriver 74.0.3729.6

BugHunterUK
  • 8,346
  • 16
  • 65
  • 121

1 Answers1

0

To eliminates lot of manual works and incompatibility issues, I would suggest you to go for WebDriverManager as it automatically downloads required binary and we do not need to set any path.

It supports browsers such as Chrome, Firefox, PhantomJS, Microsoft Edge, or Internet Explorer.

How do we use this in our project?

Only setup required is to install this package using ‘pip’.

pip install webdriver_manager

That’s it! We are all set. Just import this module in your python project and start using it.

For Chrome:

from webdriver_manager.chrome import ChromeDriverManager
from selenium import webdriver 
driver = webdriver.Chrome(executable_path=ChromeDriverManager().install())
driver.get("http://www.google.com/") 
print driver.title
driver.quit() 

For Firefox:

from webdriver_manager.firefox import GeckoDriverManager 
from selenium import webdriver
driver = webdriver.Firefox(executable_path=GeckoDriverManager().install())
driver.get("http://www.google.com/") 
print driver.title 
driver.quit()

For Edge:

from webdriver_manager.microsoft import EdgeDriverManager
from selenium import webdriver
driver = webdriver.Edge(executable_path=EdgeDriverManager().install()) 
driver.get("http://www.google.com/") 
print driver.title 
driver.quit() 

For IE:

from webdriver_manager.microsoft import IEDriverManager
from selenium import webdriver 
driver = webdriver.Ie(executable_path=IEDriverManager().install())   
driver.get("http://www.google.com/") 
print driver.title 
driver.quit()

webdriver_manager, by default, tries to download the latest version of a given driver binary. To use a specific version of driver, pass the driver version like below.

webdriver.Chrome(executable_path=ChromeDriverManager("2.42").install())
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
TheSociety
  • 1,936
  • 2
  • 8
  • 20
  • I tried this, and I tried using your examples. I get the error `Message: 'IEDriverServer.exe' executable needs to be in PATH.` (tried your IE example) copypasta. – BugHunterUK Apr 07 '19 at 13:51
  • Were you able to run chrome using WebDriverManager. – TheSociety Apr 07 '19 at 14:47
  • No `Message: 'chromedriver.exe' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home` – BugHunterUK Apr 07 '19 at 14:54
  • @TheSociety Are you aware how much overhead would be induced through `install()` if the _WebDriver_ is destroyed/re-initialized a couple of time? – undetected Selenium Apr 08 '19 at 04:40