Python Forum

Full Version: selenium not running firefox
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
so i was trying out a simple code like this:
from selenium import webdriver
browser = webdriver.Firefox()
and it returned me a huge error message:
Error:
Traceback (most recent call last): File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\common\service.py", line 76, in start stdin=PIPE) File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py", line 756, in __init__ restore_signals, start_new_session) File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py", line 1155, 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 "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\Automate the Boring Stuff with Python Course\seleniumwebdriver.py", line 2, in <module> browser = webdriver.Firefox() File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 157, in __init__ self.service.start() File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-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.
so what does this say? and what do i have to do to fix it?
not sure where your geckodriver is located on windows, but once you know where it is located, you have to add it to the system path.
this can be done in software:
see: https://stackoverflow.com/questions/2895...pt-windows

you can also use:
browser = webdriver.Firefox(firefox_options=options, capabilities=caps, executable_path=r"yourpathto/geckodriver")
I have setup in Web-scraping part-2
As both FireFox and Chrome has a headless(not loading Browser),it's okay to also have that setup.
Here a teste that it still work.
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
import time

#--| Setup
options = Options()
options.set_headless(headless=False)
caps = webdriver.DesiredCapabilities().FIREFOX
caps["marionette"] = True
browser = webdriver.Firefox(firefox_options=options, capabilities=caps, executable_path=r"C:\cmder\bin\geckodriver.exe")
#--| Parse
browser.get('https://www.python.org/')
time.sleep(2)
t = browser.find_element_by_xpath('//*[@id="dive-into-python"]/ul[2]/li[1]/div[1]/pre/code/span[1]')
print(t.text)
time.sleep(10)
browser.quit()
Output:
# Python 3: Fibonacci series up to n
If have 64-bit FireFox use 64-bit geckodriver.
options.set_headless(headless=False) Now it's loading browser,True not.
geckodriver.exe in Windows Path Environment Variables Path.
I have cmder in Path,so i use that folder.
If memory serves me right, I once had success by having the geckodriver in same location as the Python script that uses it.
Though adding it to path is probably a better choice.
I've been using snippsat's inline solution with success.
Recently, I added it to my path so it was always visible.
(Aug-31-2018, 09:05 PM)j.crater Wrote: [ -> ]If memory serves me right, I once had success by having the geckodriver in same location as the Python script that uses it.
Though adding it to path is probably a better choice.
Yes can work and not with Firefox.
Firefox setup is more difficult they have messed it more than Chrome.
Example stuff that can make trouble, DesiredCapabilities, marionette, Path, FireFox binary placement and 32/64 bit.

So Chrome always work with simple setup like this.
With cromedriver.exe is same folder as script.
from selenium import webdriver

browser = webdriver.Chrome()
url = 'https://nordic.ign.com/'
browser.get(url)
Just to do a test,to make sure it work,no Path is used here just cromedriver in same folder.
from selenium import webdriver
import time

browser = webdriver.Chrome()
url = 'https://nordic.ign.com/'
browser.get(url)
time.sleep(5)
close_cookie = browser.find_elements_by_css_selector('#_evh-ric-gc')
close_cookie[0].click()
top_storie = browser.find_elements_by_css_selector('#blogroll > li:nth-child(1) > article > h3 > a')
print(top_storie[0].text)
time.sleep(5)
browser.quit()
Output:
Wolverine's Whereabouts Revealed as the X-Men Meet a Deadly New Enemy