0

I'm trying out selenium's Simple Usage. Its code driver = webdriver.Firefox() gave an error.

This is my full code:

while True:
    try:
        from selenium import webdriver
        from selenium.webdriver.common.keys import Keys
    except:
        print('Failed to import selenium tools, retrying...')
        continue
    else:
        print('Selenium import success!')
        break

while True:
    try:
        browser = webdriver.Firefox()
    except:
        print('Open browser error: An error occured, retrying..')
        continue
    else:
        print('Success!')
        break

browser.get('http://www.python.org')
assert "Python" in browser.title

elem = browser.find_element_by_name('q')
elem.send_keys('pycon')
elem.send_keys(Keys.RETURN)

assert "No results found" not in browser.page_source

and the result:

Selenium import success!
Open browser error: An error occured, retrying..Traceback (most recent call last):
  File "C:\Users\user\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\common\service.py", line 76, in start
    stdin=PIPE)
  File "C:\Users\user\AppData\Local\Programs\Python\Python36-32\lib\subprocess.py", line 709, in __init__
    restore_signals, start_new_session)
  File "C:\Users\user\AppData\Local\Programs\Python\Python36-32\lib\subprocess.py", line 997, in _execute_child
    startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\code\python project\auto.py", line 14, in <module>
    browser = webdriver.Firefox()
  File "C:\Users\user\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 160, in __init__
    self.service.start()
  File "C:\Users\user\AppData\Local\Programs\Python\Python36-32\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: 'geckodriver' executable needs to be in PATH. 


During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\code\python project\auto.py", line 16, in <module>
    print('Open browser error: An error occured, retrying..')

The Selenium import success! result indicates that there is no problem during importing. On the other hand, browser = webdriver.Firefox() is the one causing problem.

How should I change it to make it work? Note: The file I am working on it called auto.py

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Jonathan
  • 311
  • 4
  • 14

3 Answers3

2

Download the Windows version of Geckodriver from here and place it somewhere convenient. Then when initializing the browser variable, pass the full path to geckodriver.exe like so:

browser = webdriver.Firefox(executable_path='enter_path_here')
xrisk
  • 3,790
  • 22
  • 45
0

the error message states the issue clearly: "'geckodriver' executable needs to be in PATH".

You must download geckodriver executable and make sure it is located on your PATH.

Corey Goldberg
  • 59,062
  • 28
  • 129
  • 143
0

Place your geckodriver like this :

while True:
    try:
        browser = webdriver.Firefox(executable_path = r'D:/Automation/geckodriver.exe')
    except:
        print('Open browser error: An error occured, retrying..')
        continue
    else:
        print('Success!')
        break  

Note that D:/Automation/geckodriver.exe this should be geckodriver path. Just for your simplicity use I have written this.

cruisepandey
  • 28,520
  • 6
  • 20
  • 38