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

4
  • Have you installed geckodriver? Commented Jun 22, 2018 at 15:51
  • don't use bare excepts Commented Jun 22, 2018 at 15:52
  • 1
    Did you read the error message? Did you search this site for "'geckodriver' executable needs to be in PATH"? That seems like a pretty obvious error to me. Commented Jun 22, 2018 at 15:58
  • 1
    Possible duplicate of Selenium using Python - Geckodriver executable needs to be in PATH Commented Jun 22, 2018 at 16:52

3 Answers 3

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')
Sign up to request clarification or add additional context in comments.

Comments

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.

2 Comments

What should i do after downloading??
make sure it is on your PATH.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.