24

I am trying to build a utility function to output beautiful soup code to a browser I have the following code:

def bs4_to_browser(data):

    from selenium import webdriver

    driver = webdriver.Firefox(path="F:\FirefoxPortable\Firefox.exe")
    driver.get("about:blank")

    data = '<h1>test</h1>'  # supposed to come from BeautifulSoup
    driver.execute_script('document.body.innerHTML = "{html}";'.format(html=data))

    return

when I run this I get:

TypeError at /providers/
__init__() got an unexpected keyword argument 'path'

I am using win7. How to I set the path to the portable firefox executable?

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
user1592380
  • 34,265
  • 92
  • 284
  • 515

3 Answers3

27

To set the custom path to Firefox you need to use FirefoxBinary:

from selenium.webdriver.firefox.firefox_binary import FirefoxBinary

binary = FirefoxBinary('F:\FirefoxPortable\Firefox.exe')
driver = webdriver.Firefox(firefox_binary=binary)

Or, alternatively, add F:\FirefoxPortable to the PATH environment variable and fire up Firefox in a usual way:

driver = webdriver.Firefox()
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • Thank you, thats getting it a lot closer, however I am getting a popup stating: "Your Firefox profile cannot be loaded. It may be missing or inaccessible." also in python I'm getting a webdriver error: "Message: 'The browser appears to have exited before we could connect. The output was: ' " – user1592380 Sep 08 '14 at 00:31
  • @user61629 good, what firefox version are you launching? If it is brand new (32nd) - downgrade it to at least 31 (to be safe better 28). Also, make sure you have the latest `selenium` package installed. – alecxe Sep 08 '14 at 00:33
  • @user61629 Firefox 32nd is too new for the latest selenium. Try 31st, or 28th. Let me know if it helped or not. – alecxe Sep 08 '14 at 00:47
  • I was able to find firefox portable version 31 after deleting version 32. This does work. Thank you very much! – user1592380 Sep 08 '14 at 01:25
6

By default selenium will look into the path - C:\Program Files (x86)\Mozilla Firefox\

Please install Firefox using the link - https://www.mozilla.org/en-US/firefox/new/ and try

For this, you no need to give the binary.

If you want to install Firefox in custom location then give the directory as your wish when it pops up for location. If you installed in custom location then we need to mention Firefox binary location in the code as below

from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary

binary = FirefoxBinary("C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe")
fp = webdriver.FirefoxProfile()
driver = webdriver.Firefox(firefox_binary=binary, firefox_profile=fp)
leo
  • 8,106
  • 7
  • 48
  • 80
Karthikeya
  • 324
  • 3
  • 10
-1

If you for example downloaded the chrome driver already, you can just specify the path to it like that:

from selenium import webdriver
driver = webdriver.Chrome(r'D:\\chromedriver.exe')
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
user1767754
  • 23,311
  • 18
  • 141
  • 164