0

I'm getting the following error for selenium in python. I've installed selenium by pip install selenium and then extracted selenium file to C:\Program Files\Python36.

Here is my script:

import os
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
dir = os.path.dirname('C:\chromedriver_win32')
chrome_driver_path = dir + "\chromedriver.exe"
driver = webdriver.Chrome(chrome_driver_path)
driver.implicitly_wait(30)
driver.maximize_window()
driver.get("http://www.google.com")
search_field = driver.find_element_by_name("q")
search_field.send_keys("Selenium WebDriver Interview questions")
search_field.submit()
lists= driver.find_elements_by_class_name("r")
print ("Found " + str(len(lists)) + " searches:")
driver.quit()
fakhrul
  • 27
  • 1
  • 7

1 Answers1

1

One way is to set PATH to include C:\chromedriver_win32
But I suggest you to put chromedriver.exe in the same directory of your python.exe.
set PATH

As for your own code, might as well just use one line instead:

chrome_driver_path = 'C:\\chromedriver_win32\\chromedriver.exe'  

There's no need to import and use os.

Also note that os or not, you should escape \ itself.
Eg:

>>> os.path.dirname('C:\chromedriver_win32')
'C:\\'
>>> os.path.dirname('C:\\chromedriver_win32\\')
'C:\\chromedriver_win32'
Til
  • 5,150
  • 13
  • 26
  • 34
  • already set C:\chromedriver_win32 in environment variable and tried also keeping in the same directory where python.exe is located. – fakhrul Jan 02 '19 at 07:13
  • @fakhrul Check updated part. However I suggest you put chromedriver in python's dir. And delete chrome_driver_path when initiate driver. – Til Jan 02 '19 at 07:17
  • @fakhrul Sorry I made a mistake, updated again. – Til Jan 02 '19 at 07:21