Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Selenium to work with any browser?

I'm trying to compile some code I wrote using Python and the library Selenium. However Selenium makes you specify which browser to use

e.g.

driver = webdriver.Firefox()

So if I compile the program into an .exe or other form executable, and then run it into a computer without Firefox, it won't work. So is there a way for me to tell Selenium to just use the default browser? Or is there a way for me to have a portable version of Firefox on the same program folder (kind of like the Tor Bundle does it) so it works?

I thought of the solution of creating a bunch of nested try/excepts and try every possible browser this way, but I thought that there might be a better way.

EDIT: I would also appreciate if someone pointed me in the general direction of having selenium displayed in a wxPython window.

like image 568
AngelloMaggio Avatar asked Mar 13 '14 04:03

AngelloMaggio


1 Answers

One possible logic is that

  1. First defect what system's default browser is
  2. Then in your Selenium code, use a switch, like

browser = get_default_browser_name()

if browser = firefox
    then launch firefox
if browser = chrome
    then launch chrome
if browser = ie
    then launch ie

The first step could be tricky, it's possible for C# on Windows from registry. Not sure about how to code it in Python or how to deal with other systems like Linux/Mac.

However, your idea won't work as intended after all, because Selenium version needs to match the browser version.

For example, if you have Selenium 2.40.0 in your program, but some users use Firefox Nightly (which is FF30), then it won't work, because Selenium doesn't support FF30 at the moment.

Same for Chrome, different versions of ChromeDriver support different versions of Chrome. How could you know which ChromeDriver you need to include? ChromeDriver 2 supports Chrome from 27 and above, each of them has more specific version requirements, see release notes here. What if someone uses Chrome 26 or under? More trouble.

I would suggest you to include you own portable Firefox, then specify binary location when launch browser.

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

binary = FirefoxBinary('path/to/binary')
driver = webdriver.Firefox(firefox_binary=binary)
like image 170
Yi Zeng Avatar answered Oct 26 '22 10:10

Yi Zeng