How to hide Firefox window (Selenium WebDriver)? - python

When I execute multiple test simultaneously, i don't want to keep Firefox browser window visible.. I can minimize it using selenium.minimizeWindow() but I don't want to do it.
Is there any way to hide Firefox window? I am using FireFox WebDriver.

Python
The easiest way to hide the browser is to install PhantomJS. Then, change this line:
driver = webdriver.Firefox()
to:
driver = webdriver.PhantomJS()
The rest of your code won't need to be changed and no browser will open. For debugging purposes, use driver.save_screenshot('screen.png') at different steps of your code or just switch to the Firefox webdriver again.
On Windows, you will have to specify the path to phantomjs.exe:
driver = webdriver.PhantomJS('C:\phantomjs-1.9.7-windows\phantomjs.exe')
Java
Have a look at Ghost Driver: How to run ghostdriver with Selenium using java
C#
How to hide FirefoxDriver (using Selenium) without findElement function error in PhantomDriver(headless browser)?

Just add the following code.
import os
os.environ['MOZ_HEADLESS'] = '1'
driver = webdriver.Firefox()

Finally I found the solution for those who are using windows Machine for running the Tests using any method. Well, implementation is not in Java, but you can do it very easily.
Use AutoIt tool. It has all the capability to handle windows. It is a free tool.
Install AutoIt:
http://www.autoitscript.com/site/autoit/downloads/
Open the Editor and write below code
for Hiding any window.
AutoItSetOption("WinTitleMatchMode", 2)
WinSetState("Title Of Your Window", "", #SW_HIDE)
To Unhide it, you can use below line of code.
AutoItSetOption("WinTitleMatchMode", 2)
WinSetState("Title Of Your Window", "", #SW_SHOW)
WinTitleMatchMode has different options which can be used to match Windows title.
1 = Match the title from the start (default)`
2 = Match any substring in the title
3 = Exact title match
4 = Advanced mode, see Window Titles & Text (Advanced)
So, what I've done is: I have created an .exe file of a small program and passed a parameter as a command line argument as below.
Runtime.getRuntime().exec("C:/Diiinnovation/HideNSeek.exe 0 \"" + "Mozilla Firefox" + "\"");
in HideNSeek.exe - I have below AutoIt Code:
AutoItSetOption("WinTitleMatchMode", 1)
if $CmdLine[0] > 0 Then
if $CmdLine[1] == 0 Then
WinSetState($CmdLine[2], "", #SW_HIDE)
ElseIf $CmdLine[1] == 1 Then
WinSetState($CmdLine[2], "", #SW_SHOW)
Else
EndIf
EndIf
$CmdLine[] is an array, which will have all command line parameters...
$CmdLine[0] = number of Parameter
$CmdLine[1] = 1st Parameter after Exe Name
...
If there is any space in the Window Title, then you have to use double quotes to pass it as a command line parameter like above.
Below Line of code will execute AutoIt exe and if I pass '0' in 1st parameter then it will hide the window and if I will pass '1' then it will unhide windows matching the title.
Runtime.getRuntime().exec("C:/Diiinnovation/HideNSeek.exe 0 \"" + "Mozilla Firefox" + "\"");
I hope this will help you. Thanks!

Just do (Python):
opts = webdriver.FirefoxOptions()
opts.headless = True
firefox = webdriver.Firefox(options=opts)

I used xvfb to solve the problem like this.
First, install Xvfb:
# apt-get install xvfb
on Debian/Ubuntu; or
# yum install xorg-x11-Xvfb
on Fedora/RedHat. Then, choose a display number that is unlikely to ever clash (even if you add a real display later) – something high like 99 should do. Run Xvfb on this display, with access control off:
# Xvfb :99 -ac
Now you need to ensure that your display is set to 99 before running the Selenium server (which itself launches the browser). The easiest way to do this is to export DISPLAY=:99 into the environment for Selenium. First, make sure things are working from the command line like so:
$ export DISPLAY=:99
$ firefox
or just
$ DISPLAY=:99 firefox
Below there is a link that helped me
http://www.alittlemadness.com/2008/03/05/running-selenium-headless/

The default browser of PhantomJS is IE, though many browser features do not work there. If you want to open a headless(hidden) Firefox window, you can use the new feature of Firefox 56+.
With this feature you can get a headless driver like this:
System.setProperty("webdriver.gecko.driver", firefoxDriverExePath);
FirefoxOptions options = new FirefoxOptions();
options.addArguments("--headless");
FirefoxDriver driver = new FirefoxDriver(options);
New versions of Chrome also have the headless option.

just add these and it will work if you are using chrome, also useful in firefox
from selenium.webdriver.chrome.options import Options
'''option to make driver work background'''
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')

If you're using Selenium RC or Remote WebDriver then you can run the browser instance on a remote, or virtual machine. This means that you shouldn't have to worry about hiding the browser windows as they won't be launching on your local machine.

Firefox has a headless mode. If you want to use it, you just have to set it on binary options like this:
binary = FirefoxBinary("C:/Program Files/Mozilla Firefox/firefox.exe")
options = webdriver.FirefoxOptions()
# set headless mode on
options.set_headless(True)
driver = webdriver.Firefox(firefox_binary=binary,options=options)

If you are using KDE Desktop, you can make Firefox Windows to be initially opened being minimized. That made my day to me regarding this problem. Just do the following:
Open Firefox
Click on the Firefox icon on the top left corner of the menu bar -> Advanced -> Special Application Settings...
Go to the "Size & Position" tab.
Click on "Minimized" and choose "Apply Initially" (YES).
These settings will apply for new Firefox windows from now on and you will not be bothered with pop-ups anymore when running tests with Webdriver.

I found the easiest way was to use PhantomJS, per Stéphane's suggestion. I downloaded the binary and put phantomjs in my PATH, in my case (Mac OS) in /usr/bin/. I like to retain the option of seeing what's going on so I wrapped it like this (in Python):
def new_driver():
if 'VISIBLE_WEBDRIVER' in os.environ:
return webdriver.Firefox()
else:
return webdriver.PhantomJS()
References:
http://blog.likewise.org/2013/04/webdriver-testing-with-python-and-ghostdriver/
http://www.realpython.com/blog/python/headless-selenium-testing-with-python-and-phantomjs/

Java
I had a similar problem with ChromeDriver (I needed to minimize the browser window while the tests are running). I could not find a better way to do it, so I ended up using the keyboard combination Alt+Space, N to do it. This should work only in Windows, the example uses the Java AWT Robot class to play the keyboard shortcuts:
//Alt + Space to open the window menu
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_SPACE);
robot.keyRelease(KeyEvent.VK_SPACE);
robot.keyRelease(KeyEvent.VK_ALT);
Thread.sleep(200);
// miNimize
robot.keyPress(KeyEvent.VK_N);
robot.keyRelease(KeyEvent.VK_N);

In Java, you can use HtmlUnitDriver to launch a headless browser session which will not actually open the browser.
Add the following dependency to your pom.xml (or download and reference the following):
<dependency>
<groupId>net.sourceforge.htmlunit</groupId>
<artifactId>htmlunit</artifactId>
<version>2.15</version>
</dependency>
... and test it it as you would a WebDriver driver instance:
driver = new HtmlUnitDriver();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.get("http://www.google.com");
// etc..
driver.quit();
Another similar question in SO: Avoid opening browser on remote server during selenium call

in options (Firefox options, chrome options )
set boolean headless to true by calling set_headless method.

Related

How can I dynamically submit commands to Selenium through Python?

Since Python is mostly a runtime language, would it be possible through Selenium to have something like 'wait for developer input'. In my case it would be way more effective to test code live on a web page (like offset or scrolling) than to relaunch the website each time the code does not work as expected.
Something like
(...)
driver.get(url)
while True:
command = wait_for_python_code()
# developer inputs 'elem = driver.find_element_by_class_name('myclass')\nprint(elem.text)'
# selenium prints content of myclass on the go
So basically dynamically allow the user (developer) to type any arbitrary python code, that will be fed to selenium.
NB: I am not looking for an answer to get the class_name provided by the user input, but the whole code block that could be anything (and this is for internal use so no worries about security flaws)
You can use the input() function to accept keyboard input.
If you just want to find an element with a given class, you can use this:
while True:
klass = input("Enter the element class name: ")
elem = driver.find_element_by_class_name(klass)
print(elem.text)
However if you want to allow the user to type any arbitrary python code, it is a lot more complicated.
One very handy approach to debugging a Selenium script is to return the driver object to your prompt, then you can use the driver as you would in a script, but you have access to it in your interactive python shell:
# mycode.py
from selenium import webdriver
import time
def get_driver():
options = webdriver.ChromeOptions()
driver = webdriver.Chrome(chrome_options=options)
return driver
You can now open a python shell and run this code manually
> from mycode import get_driver
> driver = get_driver
You can now debug the driver as needed:
> driver.get('https://python.org')
> driver.find_element_by_class_name('div#ICanDoAnyThingIWant')
# etc ...

Browser automatically quit after python function execution?

if I launch chrome webdriver from a python function why does it automatically close the browser window after execution and how do I prevent this?
Here's the code:
from selenium import webdriver
def open_chrome_driver():
chrome_driver = webdriver.Chrome(executable_path=r'C:/Users/User/Documents/pythonfiles/chromedriver.exe')
return chrome_driver
open_chrome_driver()
Because the python runtime will clean up all the resources it allocated for its use when the script ends.
put a breakpoint in the last line of code you want to be executed and run it in debug mode (depends on your IDE). Once it pauses, you can do whatever you want with it.
Please try the following - it should detach chrome process from chromedriver and prevent from closing.
chrome_options.add_experimental_option("detach", True)
Hope this solves your issue.
You will need to import Options from selenium.webdriver.chrome.options import Options
And off course as Davis Jahn said - you may put a breakpoint
It worked for me to simply return the chromedriver object back to a variable with the same name
chrome_driver = open_chrome_driver()

How can I open a website in my web browser using Python?

I want to open a website in my local computer's web browser (Chrome or Internet Explorer) using Python.
open("http://google.co.kr") # something like this
Is there a module that can do this for me?
The webbrowser module looks promising: https://www.youtube.com/watch?v=jU3P7qz3ZrM
import webbrowser
webbrowser.open('http://google.co.kr', new=2)
From the doc.
The webbrowser module provides a high-level interface to allow
displaying Web-based documents to users. Under most circumstances,
simply calling the open() function from this module will do the right
thing.
You have to import the module and use open() function. This will open https://nabinkhadka.com.np in the browser.
To open in new tab:
import webbrowser
webbrowser.open('https://nabinkhadka.com.np', new = 2)
Also from the doc.
If new is 0, the url is opened in the same browser window if possible.
If new is 1, a new browser window is opened if possible. If new is 2,
a new browser page (“tab”) is opened if possible
So according to the value of new, you can either open page in same browser window or in new tab etc.
Also you can specify as which browser (chrome, firebox, etc.) to open. Use get() function for this.
As the instructions state, using the open() function does work, and opens the default web browser - usually I would say: "why wouldn't I want to use Firefox?!" (my default and favorite browser)
import webbrowser as wb
wb.open_new_tab('http://www.google.com')
The above should work for the computer's default browser. However, what if you want to to open in Google Chrome?
The proper way to do this is:
import webbrowser as wb
wb.get('chrome %s').open_new_tab('http://www.google.com')
To be honest, I'm not really sure that I know the difference between 'chrome' and 'google-chrome', but apparently there is some since they've made the two different type names in the webbrowser documentation.
However, doing this didn't work right off the bat for me. Every time, I would get the error:
Traceback (most recent call last):
File "C:\Python34\programs\a_temp_testing.py", line 3, in <module>
wb.get('google-chrome')
File "C:\Python34\lib\webbrowser.py", line 51, in get
raise Error("could not locate runnable browser")
webbrowser.Error: could not locate runnable browser
To solve this, I had to add the folder for chrome.exe to System PATH. My chrome.exe executable file is found at:
C:\Program Files (x86)\Google\Chrome\Application
You should check whether it is here or not for yourself.
To add this to your Environment Variables System PATH, right click on your Windows icon and go to System. System Control Panel applet (Start - Settings - Control Panel - System). Change advanced settings, or the advanced tab, and select the button there called Environment Varaibles.
Once you click on Environment Variables here, another window will pop up. Scroll through the items, select PATH, and click edit.
Once you're in here, click New to add the folder path to your chrome.exe file. Like I said above, mine was found at:
C:\Program Files (x86)\Google\Chrome\Application
Click save and exit out of there. Then make sure you reboot your computer.
Hope this helps!
Actually it depends on what kind of uses. If you want to use it in a test-framework I highly recommend selenium-python. It is a great tool for testing automation related to web-browsers.
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("http://www.python.org")
I think it should be
import webbrowser
webbrowser.open('http://gatedin.com')
NOTE: make sure that you give http or https
if you give "www." instead of "http:" instead of opening a broser the interprete displays boolean OutPut TRUE.
here you are importing webbrowser library
I had this problem.When I define firefox path my problem had been solved.
import webbrowser
urL='https://www.python.org'
mozilla_path="C:\\Program Files\\Mozilla Firefox\\firefox.exe"
webbrowser.register('firefox', None,webbrowser.BackgroundBrowser(mozilla_path))
webbrowser.get('firefox').open_new_tab(urL)
You can simply simply achieve it with any python module that gives you an interaction with command line(cmd) like subprocess, os, etc.
but here I came up with examples on only two modules.
Here is syntax (command) cmd /c start browser_name "URL"
Example
import os
# or open with iexplore
os.system('cmd /c start iexplore "http://your_url"')
# or open with chrome
os.system('cmd /c start chrome "http://your_url"')
__import__('subprocess').getoutput('cmd /c start iexplore "http://your_url"')
You can also run the command in the cmd it will work to or use other module call
click which mainly used for writing command line utilities.
here is how
import click
click.launch('http://your_url')
Its a 2 liner! :D
You are a great programmer so never give up!
#Use web-browser.
import webbrowser as w
w.open("https://google.com")
#remember to include https://
#If you want to make a page open if you click a button do this :
from tkinter import *
#^ Imports tk
import webbrowser as w
#^ Imports wb
x = Tk()
#Makes main window
def clicked() :
w.open("https://google.com")
#Defined the click function. (We'll use this later.)
link = Button(x, text="Click Me!", command=clicked)
link.pack(pady=20, padx=20)
#Our button
x.mainloop()
#Tkinter mainloop
If you want to open a specific browser (e.g. Chrome and Chromium) with command line options like full screen or kiosk mode and also want to be able to kill it later on, then this might work for you:
from threading import Timer
from time import sleep
import subprocess
import platform
# Hint 1: to enable F11 use --start-fullscreen instead of --kiosk, otherwise Alt+F4 to close the browser
# Hint 2: fullscreen will only work if chrome is not already running
platform_browser = {
'Windows': r'"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --kiosk http://stackoverflow.com',
'Linux' : ['/usr/bin/chromium-browser', '--kiosk', 'http://stackoverflow.com']
}
browser = None
def open_browser():
global browser
platform_name = platform.system()
if platform_name in platform_browser:
browser = subprocess.Popen(platform_browser[platform_name])
else:
print(":-(")
Timer(1, open_browser).start() # delayed start, give e.g. your own web server time to launch
sleep(20) # start e.g. your python web server here instead
browser.kill()
If you want to open any website first you need to import a module called "webbrowser". Then just use webbrowser.open() to open a website.
e.g.
import webbrowser
webbrowser.open('https://yashprogrammer.wordpress.com/', new= 2)

Getting the current active tab in google chrome, using python [duplicate]

How can my Python script get the URL of the currently active Google Chrome tab in Windows? This has to be done without interrupting the user, so sending key strokes to copy/paste is not an option.
First, you need to download and install pywin32. Import these modules in your script:
import win32gui
import win32con
If Google Chrome is the currently active window, first get the window handle by:
hwnd = win32gui.GetForegroundWindow()
(Otherwise, find the Google Chrome window handle by using win32gui.FindWindow. Windows Detective is handy when finding out class names for windows.)
It seems the only way to get the URL is to get the text in the "omnibox" (address bar). This is usually the tab's URL, but could also be any partial URL or search string that the user is currently typing.
Also, the URL in the omnibox won't include the "http://" prefix unless the user has typed it explicitly (and not yet pressed enter), but it will in fact include "https://" or "ftp://" if those protocols are used.
So, we find the omnibox child window inside the current Chrome window:
omniboxHwnd = win32gui.FindWindowEx(hwnd, 0, 'Chrome_OmniboxView', None)
This will of course break if the Google Chrome team decides to rename their window classes.
And then we get the "window text" of the omnibox, which doesn't seem to work with win32gui.GetWindowText for me. Good thing there's an alternative that does work:
def getWindowText(hwnd):
buf_size = 1 + win32gui.SendMessage(hwnd, win32con.WM_GETTEXTLENGTH, 0, 0)
buf = win32gui.PyMakeBuffer(buf_size)
win32gui.SendMessage(hwnd, win32con.WM_GETTEXT, buf_size, buf)
return str(buf)
This little function sends the WM_GETTEXT message to the window and returns the window text (in this case, the text in the omnibox).
There you go!
Christian's answer did not work for me as internal structure of Chrome changed entirely and you can't really access elements of Chrome window using win32gui anymore.
The only possible way I managed to find was through UI Automation API, which has this python wrapper with some examples of usage
Run this and switch to Chrome window you want to grab address from:
from time import sleep
import uiautomation as automation
if __name__ == '__main__':
sleep(3)
control = automation.GetFocusedControl()
controlList = []
while control:
controlList.insert(0, control)
control = control.GetParentControl()
if len(controlList) == 1:
control = controlList[0]
else:
control = controlList[1]
address_control = automation.FindControl(control, lambda c, d: isinstance(c, automation.EditControl) and "Address and search bar" in c.Name)
print address_control.CurrentValue()
I quite new to StackOverFlow so apologies if the comment is out of tone.
After looking at :
Selenium,
launching chrome://History directly,
doing some keyboard emulation : copy/paste with Pywinauto,
trying to use SOCK_RAW connections to capture the headers as per the Network tab of the DevTool (this one was very interesting),
trying to get text of the omnibus/searchBar window element,
closing and reopening chrome to read the history tables,
....
I resulted in copy/pasting the History file itself (\AppData\Local\Google\Chrome\User Data\Default\History) into my application folder when the title of the window (retrieved using the hwnd + win32) is missing from "my" urls table.
This can be done even if the sqlite db is locked and does not interfere with the user experience.
Very basic solution that requires : sqlite3, psutil, win32gui.
Hope that helps.

How do I get the URL of the active Google Chrome tab in Windows?

How can my Python script get the URL of the currently active Google Chrome tab in Windows? This has to be done without interrupting the user, so sending key strokes to copy/paste is not an option.
First, you need to download and install pywin32. Import these modules in your script:
import win32gui
import win32con
If Google Chrome is the currently active window, first get the window handle by:
hwnd = win32gui.GetForegroundWindow()
(Otherwise, find the Google Chrome window handle by using win32gui.FindWindow. Windows Detective is handy when finding out class names for windows.)
It seems the only way to get the URL is to get the text in the "omnibox" (address bar). This is usually the tab's URL, but could also be any partial URL or search string that the user is currently typing.
Also, the URL in the omnibox won't include the "http://" prefix unless the user has typed it explicitly (and not yet pressed enter), but it will in fact include "https://" or "ftp://" if those protocols are used.
So, we find the omnibox child window inside the current Chrome window:
omniboxHwnd = win32gui.FindWindowEx(hwnd, 0, 'Chrome_OmniboxView', None)
This will of course break if the Google Chrome team decides to rename their window classes.
And then we get the "window text" of the omnibox, which doesn't seem to work with win32gui.GetWindowText for me. Good thing there's an alternative that does work:
def getWindowText(hwnd):
buf_size = 1 + win32gui.SendMessage(hwnd, win32con.WM_GETTEXTLENGTH, 0, 0)
buf = win32gui.PyMakeBuffer(buf_size)
win32gui.SendMessage(hwnd, win32con.WM_GETTEXT, buf_size, buf)
return str(buf)
This little function sends the WM_GETTEXT message to the window and returns the window text (in this case, the text in the omnibox).
There you go!
Christian's answer did not work for me as internal structure of Chrome changed entirely and you can't really access elements of Chrome window using win32gui anymore.
The only possible way I managed to find was through UI Automation API, which has this python wrapper with some examples of usage
Run this and switch to Chrome window you want to grab address from:
from time import sleep
import uiautomation as automation
if __name__ == '__main__':
sleep(3)
control = automation.GetFocusedControl()
controlList = []
while control:
controlList.insert(0, control)
control = control.GetParentControl()
if len(controlList) == 1:
control = controlList[0]
else:
control = controlList[1]
address_control = automation.FindControl(control, lambda c, d: isinstance(c, automation.EditControl) and "Address and search bar" in c.Name)
print address_control.CurrentValue()
I quite new to StackOverFlow so apologies if the comment is out of tone.
After looking at :
Selenium,
launching chrome://History directly,
doing some keyboard emulation : copy/paste with Pywinauto,
trying to use SOCK_RAW connections to capture the headers as per the Network tab of the DevTool (this one was very interesting),
trying to get text of the omnibus/searchBar window element,
closing and reopening chrome to read the history tables,
....
I resulted in copy/pasting the History file itself (\AppData\Local\Google\Chrome\User Data\Default\History) into my application folder when the title of the window (retrieved using the hwnd + win32) is missing from "my" urls table.
This can be done even if the sqlite db is locked and does not interfere with the user experience.
Very basic solution that requires : sqlite3, psutil, win32gui.
Hope that helps.

Categories