Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Launch Firefox with Python 3.x

I'm trying to write a script that will launch firefox for me, open up google in a new tab, and be able to do a search (for instance, www.espn.com). I'm currently trying to achieve this by using the webbrowser module, however I run into an error each time that I try launching Firefox from the script. Also, firefox is not my default browser.

import webbrowser
webbrowser.get('firefox').open_new_tab('http://www.google.com')

Whenever I run this I get the following error:

Traceback (most recent call last):
  File "C:/Python33/test Bing.py", line 6, in <module>
    webbrowser.get('firefox').open_new_tab('http://www.google.com')
  File "C:\Python33\lib\webbrowser.py", line 53, in get
    raise Error("could not locate runnable browser")
webbrowser.Error: could not locate runnable browser

I'm unsure why the script is struggling to locate the firefox.exe I have also tried specifying in 'firefox' the actual location of firefox.exe in c: however I still get the same error.

I'm sure that there's a small error in my code that I currently can't see, if someone could help point out what I'm doing wrong I'd greatly appreciate it!

like image 722
Valrok Avatar asked Aug 26 '13 13:08

Valrok


People also ask

How do I open firefox with Python?

To make Firefox work with Python selenium, you need to install the geckodriver. The geckodriver driver will start the real firefox browser and supports Javascript. Take a look at the selenium firefox code. First import the webdriver, then make it start firefox.


2 Answers

I have Firefox installed on my Windows machine as well, and have the same error.

If you run the following two lines in IDLE:

import webbrowser
print webbrowser._browsers # or print(webbrowser._browsers) for Python 3.x

Then you'll get a dict of available browser controllers, as said in the source code. On my system it prints:

{'windows-default': [<class 'webbrowser.WindowsDefault'>, None], 
    'c:\\program files\\internet explorer\\iexplore.exe': [None, <webbrowser.BackgroundBrowser object at 0x01BAF6B0>]
}

I have IE, Chrome and Firefox, but only "Default" and "Internet Explorer" appear here. According to the documentation, the keys 'firefox' and 'mozilla' should work, but they are absent here.

From lines 539-563 in the source code, Python will only register a browser if its corresponding key (e.g. 'firefox' or 'chrome') is a valid command using _iscommand(cmd).

I added the Firefox path to %path% and restarted IDLE. _iscommand('firefox') returns True and webbrowser.get('firefox) returns a <webbrowser.BackgroundBrowser object at 0x01BDF7F0>. However, webbrowser._iscommand("chrome") still returns False and webbrowser.get("chrome") still throws the exception.

Conclusion

You'll have to add the Firefox path to the %path% variable first, or make the assumption that Firefox is the default browser.

like image 93
SimonT Avatar answered Oct 07 '22 05:10

SimonT


Make sure the Firefox executable is on the path (%PATH% on Windows, $PATH on Linux).

like image 43
Aaron Digulla Avatar answered Oct 07 '22 04:10

Aaron Digulla