Selenium include mozilla instance - python

I have a server on which I want to build a script to login to page which is using javascript. I want to use python selenium to achieve the same.
We have a shared drive which contains all the installed binaries and the same has to be included. So when running a python program I won't be using my #!/usr/bin/python instead efs/path../python, similarly all the packages are to be included in this ways. sys.path.append("/efs/path.../selenium-egg-info"). This works good, but as selenium would need firefox included, I could see mozilla in the path, but where are it's binary, exactly which folder to include inside mozilla.

You can think Selenium as launching 'firefox' behind the scenes. You won't see it but it's there and then accordingly opening up the webpage and manipulating things.
How do you think it does all that cool stuff without writing explicit url header etc. So for that you need to have a firefox installed with a physical display(monitor) installed.
You can fake a pyhsical terminal it's just input/output but you AFAIK you need to have a firefox installed. Sad news but that's the way it is.

You don't need firefox executable since it comes with the Selenium
Firefox driver is included in the selenium-server-stanalone.jar available in the downloads. The driver comes in the form of an xpi (firefox extension) which is added to the firefox profile when you start a new instance of FirefoxDriver.
See my another answer here

Related

Can Python install files into PATH?

In order to install Selenium, step 3 on (this site indicates needing to install the chromedriver file in your PATH. I am on a work computer that does not have access to the system PATH directly. I have tried listing in the local PATH (I'm on Windows 7) variable chain like so: C:\Users\mknerr\AppData\Local\Programs\Python\Python36-32\Scripts\;C:\Users\mknerr\AppData\Local\Programs\Python\Python36-32\;C:\Users\mknerr\AppData\Local\atom\bin;C:\Users\mknerr\Programs\ChromeDriver\
(The .exe is in the ChromeDriver folder)
When I run the script with webDriver.Chrome(), I still get a WebDriverException that chromedriver needs to be in my PATH. If anyone has an idea why this isn't working from my local PATH, I'd love to hear them.
However, my real question is when I distribute this script to the rest of my team, they will likely have the same issue since my script will be calling chromedriver, which none of them will have installed, much less in their PATH. Can Python directly install a program or dependency in the PATH so they don't have to go directly accessing environment variables? I can guarantee nobody is going to feel comfortable doing that.
You can place chromedriver.exe in the same folder as the executable. Just run the program with the driver right next to it.
In our internal automation framework, we actually just distribute the Chromedriver executable as part of the framework, in the same folder as the framework's entry point.
Then, whenever we need a browser session, we do something similar to this:
import os
from selenium import webdriver
chromedriver_location = os.path.join(
os.path.dirname(os.path.abspath(__file__)), 'chromedriver.exe')
context.browser = webdriver.Chrome(executable_path=chromedriver_location)
chromedriver.exe is the default Windows name of the executable, of course; change to whatever you need.
This avoids any user setup other than installing the framework itself. No messing with PATH or any other local files.

Sublime Text plugin can’t find my default browser

TL;DR: Sublime Text gets a different response from webbrowser._browsers than my terminal.
This has been driving me nuts. I use a plugin, GitLink which will open a GitHub link from your current file. It relies on Python’s webbrowser tool to open the url. The problem is my default browser is Chrome, but it keeps opening Firefox.
In my terminal, if I launch into python or python3, webbrowser.open_new_tab('https://stackoverflow.com') will correctly launch in Chrome. webbrowser._browsers will correctly list 'chrome' as one of my browsers.
However, in the Sublime Text console, webbrowser._browsers is missing Chrome. It lists all the other browser save for the one I actually want. What gives? How is Sublime Text getting a different list than when I run python in my terminal? How do I get it to match?
There are several things you could try.
1. Set the BROWSER environment variable:
As Keith Hall implied this problem might be solved by setting your BROWSER environment variable. If you don't know how look it up for your version - OSX changed how environment variables get set at some point so there are different ways of doing this for different versions of OSX. However, I am on Linux, and my BROWSER environment variable is not set and ST always opens urls in my default browser, this includes calls to webbrowser.open_new_tab() which (as I assume you saw) is what GitLink uses to open urls, so this may not solve the problem.
2. Modify GitLink (your installed version):
First test if this will work - works fine on Linux.
Copy and paste the following 2 lines into the ST console:
import webbrowser
webbrowser.get("chrome").open_new_tab("http://www.google.com")
If that does not open Chrome with Google.com try:
# google-chrome: Chrome variant.
webbrowser.get("google-chrome").open_new_tab("http://www.google.com")
# macosx: uses the OSX default browser.
webbrowser.get("macosx").open_new_tab("http://www.google.com")
# links: generic; doubtful but worth trying at this stage.
webbrowser.get("links").open_new_tab("http://www.google.com")
The full list of possible values may be helpful.
Another possibility is to use the full path instead, see this StackOverflow answer or try:
# Replace path with your path to Chrome if necessary.
webbrowser.register('chrome_path', None, webbrowser.BackgroundBrowser("/Applications/Google Chrome.app"))
webbrowser.get('chrome_path').open_new_tab(url)
Once that is working:
Install the PackageResourceViewer plugin; after it is installed...
Open the Command Palette and select: PackageResourceViewer: Open Resource
In the list of packages select: GitLink
In the list of files select: GitLink.py
The file GitLink.py will open...
If you save this file (nothing will happen at all if you close it without saving) then a copy of GitLink.py will get saved on your system in this location: ST_CONFIG/Packages/GitLink/GitLink.py - this version of the file will override the version of GitLink.py which is stored in the Gitlink.sublime-package file which Package Control would have installed in the Installed Packages folder. Even if the GitLink package gets updated the version in the .sublime-package file will still get overridden. Not a problem, all you need to do to get rid of the changes made is to delete the folder ST_CONFIG/Packages/GitLink/ which contains the GitLink.py file and ST will start using the version from the .sublime-package file again.
The modification is easy:
Scroll down to the bottom of GitLink.py where you will see the lines:
if(args['web']):
webbrowser.open_new_tab(url)
Just change the webbrowser.open_new_tab(url) line to the following (replacing "chrome" if necessary with the value which worked in the console):
if(args['web']):
webbrowser.get("chrome").open_new_tab(url)
Save the file, the plugin should be updated immediately by ST (check the console for the "reloading plugin" message to be sure if you want). The plugin should now open your urls in Chrome.
3. Open an issue on GitLink's GitHub page:
The issue page is here. State your problem and request a setting be added so that users can specify which browser Python's webbrowser module should use.
I suggest you do this anyway and add a link to this StackOverflow page to your issue for reference.

Force selenium to use the portable firefox application

I have installed firefox 14 and has firefox portable version 25.0.1 on the machine, where I run tests for a web site.
Due to a limitation in the site I'm testing, I cannot run my tests on firefox 14 installation. Also I cannot upgrade the firefox 14 installation.
So I'm looking into a solution where I can use this portable firefox version instead of the installed firefox 14 version.
How should I force selenium to use this portable version and not the installed version? If someone could direct me to some descriptive article/blog that would be great.
My code goes like:-
* Variables *
${SELENIUM_HUB} remote_url=http://127.0.0.1:4444/wd/hub
${BROWSER} firefox D:\\Firefox Portable\\FirefoxPortable\\firefox.exe
${CLIENT_URL} https://abcd.aline.local
Open Browser ${CLIENT_URL} ${BROWSER} ${SELENIUM_HUB}
Specifying path as, D:/Firefox Portable/FirefoxPortable/firefox.exe does not work because '/' get's removed. Any thoughts?
PS: python is the used language
You can specify the path to the firefox binary you want with the FirefoxBinary class passed as the firefox_binary parameter when instantiating your Firefox webdriver.
http://selenium.googlecode.com/svn/trunk/docs/api/py/webdriver_firefox/selenium.webdriver.firefox.firefox_binary.html
and
http://selenium.googlecode.com/svn/trunk/docs/api/py/webdriver_firefox/selenium.webdriver.firefox.webdriver.html#module-selenium.webdriver.firefox.webdriver
Make sure the path to the binary is correct with something like:
from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
firefox_binary = FirefoxBinary("D:\\Firefox Portable\\FirefoxPortable\\firefox.exe")
driver = webdriver.Firefox(firefox_binary=firefox_binary)
Using robotframework something like:
${firefox_binary}= Evaluate sys.modules['selenium.webdriver.firefox.firefox_binary'].FirefoxBinary("D:\\Firefox Portable\\FirefoxPortable\\firefox.exe") sys, selenium.webdriver.firefox_binary
Create Webdriver Firefox firefox_binary=${firefox_binary}
may work.
Selenium2Library does not let you specify browser path in the Open Browser keyword, but it does have remote_url argument that can be useful. Before Selenium2Library got proper PhantomJS support the way to use PhantomJS was through that remote_url, like this http://spage.fi/phantomjs
So in theory we should be able to use portable Firefox first by launching our Firefox and then connecting to that using the remote_url. Something like this.
Start Process c:\\path\\to\\portable\\firefox.exe
Open Browser http://google.com firefox main browser http://localhost:${firefox webdriver port}
The problem is that I do not know what webdriver port Firefox uses by default or how to specify it. Also installing the webdriver.xpi addon for Firefox might be necessary. The add-on can be found from here C:\Python27\Lib\site-packages\selenium\webdriver\firefox or what ever is the place where your python installation is.
There is a Create Webdriver keyword in Selenium2Library which does allow us to specify firefox_binary (among other arguments). So in theory
Create Webdriver Firefox firefox_binary=c:\\path\\to\\portable\\firefox.exe
Should work, but all I get from that is "AttributeError: 'str' object has no attribute 'launch_browser'".
Sorry that I could not figure out way to do this, but by digging a bit deeper about Firefox webdriver port or how Create Webdriver actually works you might get further.

Selenium with GhostDriver in Python on Windows

This is embarrassing to ask because it seems like something with so slim chance of error. I wouldn't think this would be difficult, but I've been plugging away at this for almost 3 hours now and it's giving me a headache. I've read several dozen stackoverflow threads and Google threads.
I've installed PhantomJS, added it to my System Variables PATH, and it works properly in the command line. I also installed Selenium earlier with easy_install.
The error I get is:
__init__ C:\Python27\lib\site-packages\selenium-2.39.0-py2.7.egg\selenium\webdriver\phantomjs\webdriver.py 50
start C:\Python27\lib\site-packages\selenium-2.39.0-py2.7.egg\selenium\webdriver\phantomjs\service.py 66
WebDriverException: Message: 'Unable to start phantomjs with ghostdriver.' ; Screenshot: available via screen
Here's my code:
from selenium import webdriver
driver = webdriver.PhantomJS(executable_path="C:\Python27\misc\phantomjs\phantomjs.exe")
I also tried:
from selenium import webdriver
driver = webdriver.PhantomJS()
I get the same error message. This has to be something simple that I'm doing wrong. I'd appreciate any comments or answers.
Windows 7 64 bit
Python 2.7
This may have been a version issue for you, but since I just went through setting this up on my Windows 7 PC without issues I'm going to share my 'journey' here.
First of, I'm more used to the Mac/Linux Terminal and having the python package manager pip at my disposal is essential to me. After installing Python 2.7.8 and adding ;c:\Python27 to my PATH I noticed that pip isn't included with Python versions lower than 2.7.9, so I had to add it myself. Afterwards I added ;c:\Python27\Scripts to my PATH.
After that fetching the python package selenium was as easy as typing the following into the cmd:
pip install selenium
Then I downloaded the phantomjs-1.9.7-windows.zip from here, unzipped it and placed it here:
C:\Python27\misc\phantomjs-1.9.7-windows\phantomjs.exe
From there I had a working Python 2.7/Selenium Webdriver/PhantomJS example for Windows 7.
from selenium import webdriver
import os
phantomjs_path = "C:\Python27\misc\phantomjs-1.9.7-windows\phantomjs.exe"
browser = webdriver.PhantomJS(executable_path=phantomjs_path, service_log_path=os.path.devnull)
browser.set_window_size(1400, 1000)
browser.get("https://stackoverflow.com/")
print browser.title
Note that I added the argument service_log_path=os.path.devnull to the function webdriver.PhantomJS() to prevent PhantomJS from creating a ghostdriver.log in the directory of the python file being executed.
I had the same problem running Python 3.4 on Windows Server 2012 R2. PhantomJS was failing to create the ghostdriver.log file. I followed these steps that fixed it for me:
Made sure phantomjs.exe was not showing "Blocked" on the File Properties|Security tab, and ran it as standalone app to confirm.
Deleted an old copy of the ghostdriver.log file that was in the same directory.
Ran python REPL from the console while checking to see if the code that instantiated the driver was getting called successfully.
browser = webdriver.PhantomJS(executable_path='phantomjs.exe', desired_capabilities=argdc, service_args=svc_args)
Do you any other file or directory with a same name , or a file of coding (like .. phantomjs.py) which you have named same as phantomjs is so then rename it to something else. i hope it works

Add option to right-click menu in Firefox

Hi I'm studying Python and I've started my first little project.
The first thing that I want to do is to add an item to the right click menu of Firefox. So, when I right-click a link that item will be available and when I click it some Python code will be called in order to "do something" with that URL.
Do I have to create a Firefox extension to do this? Can I specify in that extension the Python code that should be called?
Yes, you will need to write a Firefox extension. This is a getting-started tutorial. To call your external Python code you will use nsIProcess.
Why have you chosen to do this in Python? Since Firefox extensions do their thinking in JavaScript anyway, if you use that then you don't have to call external processes. In particular, you won't need to install Python for your extension to work!
I think it is not possible. Normal FF extensions are afaik written in XUL and Javascript and therefore can not call other (non JS-) code.

Categories