python - Allow Selenium Webdriver to find Firefox path in cygwin and standard installs -


i running win2k8 ec2 instance purpose of running few browser automation tasks after deployment fabric on *nux box.

my script works on mac , linux presents following error under cygwin cygwin python:

file "/home/myuser/.virtualenvs/myproject/lib/python2.7/site-packages/selenium/webdriver/firefox/firefox_binary.py", line 141, in _get_firefox_start_cmd     " please specify firefox binary location or install firefox") runtimeerror: not find firefox in system path. please specify firefox binary location or install firefox 

there known bug/lack of interest in supporting webdriver under cygwin (selenium).

a fellow user more helpful , has solution here: https://stackoverflow.com/a/11104952/1668057

that method seems break code under mac/*nix.

how can implement , keep code portable?

(my selenium installed pip, prefer override method edit module files)

edit:

seeing more pythonic way suggested jeff's answer, came following (note script subclassed/overrode firefoxprofile class disabling images):

from selenium.common.exceptions import nosuchelementexception selenium.webdriver.firefox.firefox_profile import firefoxprofile subprocess import popen, pipe  class cygwinfirefoxprofile(firefoxprofile):      @property     def path(self):          path = self.profile_dir          try:             proc = popen(['cygpath','-d',path], stdout=pipe, stderr=pipe)             stdout, stderr = proc.communicate()             path = stdout.split('\n', 1)[0]             print("cygwin path found")          except oserror:             print("no cygwin path found")          return path  class carserviceonlinebookingstest(unittest.testcase):          def setup(self):          firefoxprofile = cygwinfirefoxprofile()          ## disable css         firefoxprofile.set_preference('permissions.default.stylesheet', 2)         ## disable images         firefoxprofile.set_preference('permissions.default.image', 2)         ## disable flash         firefoxprofile.set_preference('dom.ipc.plugins.enabled.libflashplayer.so', 'false')          self.driver = webdriver.firefox(firefoxprofile) 

on mac, catches exception , continues normal, on win2k8 box, cygwin path detected, still fails following error:

traceback (most recent call last):   file "myscript.py", line 45, in setup     self.driver = webdriver.firefox(firefoxprofile)   file "/home/myuser/.virtualenvs/myenv/lib/python2.7/site-packages/selenium/webdriver/firefox/webdriver.py", line 50, in __init__     self.binary = firefoxbinary()   file "/home/myuser/.virtualenvs/myenv/lib/python2.7/site-packages/selenium/webdriver/firefox/firefox_binary.py", line 33, in __init__     self._start_cmd = self._get_firefox_start_cmd()   file "/home/myuser/.virtualenvs/myenv/lib/python2.7/site-packages/selenium/webdriver/firefox/firefox_binary.py", line 141, in _get_firefox_start_cmd     " please specify firefox binary location or install firefox") runtimeerror: not find firefox in system path. please specify firefox binary location or install firefox 

i'm not @ familiar popen or i'm expecting returned positive result. ie, should expect c:\program files (x86)\firefox\firefox.exe?

where next debug step?

edit #2:

executing command cygwin bash shell open firefox:

/cygdrive/c/program\ files\ \(x86\)/mozilla\ firefox/firefox.exe

i think next step hardcode script , see if allow selenium launch firefox either locally through cygwin bash or remotely via ssh...

ok, kind of blatantly obvious, after manually setting path variable on win2k8 cygwin, code jeff's answer works , happily running firefox on win2k8 machine via remote linux machine.

i hadn't manually set path, thinking cheating, can done part of fabric script if want full automation...

here code working fine on both mac , windows:

from selenium.common.exceptions import nosuchelementexception selenium.webdriver.firefox.firefox_profile import firefoxprofile subprocess import popen, pipe  class cygwinfirefoxprofile(firefoxprofile):      @property     def path(self):          path = self.profile_dir          # cygwin requires manually specify firefox path below:         # path=/cygdrive/c/program\ files\ \(x86\)/mozilla\ firefox/:$path         try:             proc = popen(['cygpath','-d',path], stdout=pipe, stderr=pipe)             stdout, stderr = proc.communicate()             path = stdout.split('\n', 1)[0]          except oserror:             print("no cygwin path found")          return path  class carserviceonlinebookingstest(unittest.testcase):          def setup(self):          firefoxprofile = cygwinfirefoxprofile()          ## disable css         firefoxprofile.set_preference('permissions.default.stylesheet', 2)         ## disable images         firefoxprofile.set_preference('permissions.default.image', 2)         ## disable flash         firefoxprofile.set_preference('dom.ipc.plugins.enabled.libflashplayer.so', 'false')          self.driver = webdriver.firefox(firefoxprofile) 

hopefully journey doing similar.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -