How to start a Selenium Firefox web browser minimized? - python

My python script is in a loop and runs every half an hour, when ever the code runs it opens Firefox browser. So if I am working/ looking at any other screen all of a sudden the browser window pops up. I want my browser to either start minimized or disappear but my code should execute
while(True):
driver = webdriver.Firefox()
driver.get("http://iiitb.campusmetalink.com/cml/pages/setup/ModuleHome.jsf")
driver.find_element_by_name("recqIDId:j_id15").send_keys("IIITB")
driver.find_element_by_name("recqIDId:j_id17").send_keys("username")
driver.find_element_by_name("recqIDId:j_id19").send_keys("password")
driver.find_element_by_name("recqIDId:j_id25").click()
WebDriverWait(driver, 100)
driver.find_element_by_xpath('//*[#id="moduleForm:j_id40_body"]/table/tbody/tr/td[2]/table/tbody/tr/td/table/tbody/tr[6]/td[2]/a').click()
driver.find_element_by_xpath('//*[#id="scheduleForm:coltrm"]').send_keys("2016TERM2")
driver.find_element_by_xpath('//*[#id="scheduleForm:search"]').click()
elements = driver.find_elements_by_xpath('//*[#id="scheduleForm:svres:tb"]/tr')
if( len(elements) > 6):
os.system("C:/wamp64/bin/php/php5.6.16/php C:/wamp64/www/srishti/space_auth/Source/Source_code/public/mail_includes.php")
print elements
driver.close()
time.sleep(1800)

Unfortunately selenium webdriver does not provide any built in function to minimize the window .
However you can position your window out of sight by using below code.
driver.manage().window().setPosition(new Point(-2000, 0));

While Selenium does provide an inbuilt function to maximize the window, unfortunately it doesn't provide the same functionality to minimize the window.
The below code will move the browser window out of sight, but I don't know if this will fulfil your requirements.
driver.set_window_position(0, 0)

As other people already said: selenium webdriver does not provide any built in function to minimize the window BUT there are few roundabouts way to do it.
One of my favourite way is using win32gui and win32con:
import win32gui, win32con
win32gui.ShowWindow(win32gui.GetForegroundWindow(), win32con.SW_MINIMIZE)
The bad side of this code is, that the browser must be on the foreground.

Related

How can I click the close button in Gmail with Python + Selenium?

I want to click on the button to close the message that is being written.
I try with this code, but it does not work for me:
driver.find_element_by_xpath('//input[#type="Cerrar"]').click()
Can you help me? Thanks very much.
You need to click this element: //img[#alt="Close"]
So, the Selenium python command to do this is
driver.find_element_by_xpath('//img[#alt="Close"]').click()
unless your code needs to use selenium i think that pyautogui would be better for this. it can move your mouse to a specific coordinate or look for the close button and then click it
import pyautogui
pyautogui.moveTo(x=(your x), y=(your y))
pyautogui.click()
i dont know much about selenium so im sorry i cant use that to help you but i hope this helps.
The built-in method close:
The close() method is used to close the current browser window on which the focus is set, on the other hand, the quit() method essentially calls the driver. dispose of a method that successively closes all the browser windows and ends the WebDriver session graciously.
You want to use click when interacting with elements
driver.find_element_by_xpath('//input[#type="Cerrar"]').click()
If that doesn't work, please share the HTML code.

Is there 'as if' maximized mode of browser in Webdriver Selenium, but in reality minimized?

I need a browser in Selenium to work as if it's maximized, so that website that I process perceived it as maximized, but I want it really to be minimized, so that I could work in parallel while the script executes. The simple: driver.maximize_window() just maximizes windows.
So, is there a way for a window of browser to be maximized for website, but in reality minimized?
Several approaches here, depending on what your needs are.
First, most browser allow setting screen size when they are fired up. See details here How do I set browser width and height in Selenium WebDriver? Chrome, for example, can be controlled from outside of the selenium script either with ini files where you set your screen size properties or via command line arguments. See this page for specific args for chromium for example: https://peter.sh/experiments/chromium-command-line-switches/
Secondly, you could start your browser in headless mode and restrict it a specific size. If you do so, your code will run in the headless browser, meaning you can't see it on a real screen. See the miriad of stackoverflow question addressing this, including this one: How to configure ChromeDriver to initiate Chrome browser in Headless mode through Selenium?
Thirdly, you could start a virtual display of a certain size and have your driver maximize the browser in all the tests. Tools like xvfb are a god send here. How do I run Selenium in Xvfb?
Lastly, you could use a real display that's set in the resolution/dpi you need. See answers like this Resize display resolution using python with cross platform support
You can try just running it in a small window instead of Maximized or Minimized.
Don't tell the browser to do anything in terms of size.
So where you tell Selenium
browser.maximize_window()
Erase or comment it out, and run the script that way.
It's either that or use multiple screens.
If you want to work while WebDriver is executed, you can created a virtual instance (with e.g. VirtualBox or HyperV) an run test suite in VM.
Maximized is just a browser size from the site perspective. Set the browser size to the screen resolution of your desktop and minimize the browser.

When i minimize chrome window, selenium stops [duplicate]

I have a project to get some information from website.
I want to look at the process inside the chrome window, So I can't use headless browser. But sometimes I want to minimize the chrome window.
But I found the selenium would go wrong after I minimize the chrome window manually, but sometimes not. When go wrong, exception
element is not clickable at the point, other element will receive the click
will be raised, or sometimes selenium just stop.
I have searched for a long time that some people said that the chrome window should be focused on and can't be minimized by clicking '-' on the window title bar. And the alternative solution is:
web.set_window_position(-2000,-2000)
To make the window move out the screen.
And someone says by simulating shortcuts to minimize the window. But I think it's the same as click '-' manually, am I wrong?
My question is :
Does selenium really requires chrome window not minimized? Why sometimes it can run selenium normally after minimized but sometimes not?
If I use
set_window_position(-2000,-2000)
to move the window invisible, then I click the window icon on the bottom of os (I'm sorry that I don't know what it's called). Normally when clicked, the window will be minimized. So as for this chrome window, Will it be considered as minimized window and go wrong?
I am really sorry for my poor English. I hope I have a clear description of my problem.
Environment:
python 3.6
chrome 66.0
selenium 3.11.0
windows server 2012
Edit to add code:
wait.WebDriverWait(driver,100000).until(EC.visibility_of_element_located((By.ID,'commMgrCompositionMessage')))
textArea = driver.find_element_by_id('commMgrCompositionMessage')
driver.execute_script("arguments[0].value="+"'"+modelStr+"';",textArea)
time.sleep(1)
wait.WebDriverWait(driver,10000).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR,'#sendemail_label')))
allSendMailLabel = driver.find_elements_by_css_selector('#sendemail')
allSendMailLabel = allSendMailLabel[1]
driver.execute_script("arguments[0].click();", allSendMailLabel)
If you see the question here Debugging "Element is not clickable at point" error, there is a bug in the chromedriver that causes this. The issue for it was created here. There is a workaround listed in the 27th comment, but what you can do is switch to the firefox driver and see if that works. Minimised windows should not cause a problem otherwise.

Selenium PhantomJS throws EelementNotVisible while Firefox is completely fine during combo box selection

So I have a website which has a combo that I need to select an item from, now the problem is it's a bit untraditional and doesn't have option's as elements but instead it has divs.
So I need my program to click combo box then wait (the best way to do this I found is via implicitly_wait(3)# 3 seconds) and then click the box element I need. Firefox is doing a great job with it but PhantomJS seem to throw:
Selenium.common.exceptions.ElementNotVisibleException:
Message: 'Error Message => \'Element is not currently visible and may not be manipulated\'
I'm not sure what's the cause of it, but I suspect that PhantomJS fails to correctly wait via implicitly_wait for some reason and tries to select non-visible element.
Any idea how to approach this without forced thread sleep?
Yup, your issue sounds exactly something I've just fixed in a UI test that was starting to anoy me. Quite a complex one, that passed on all browsers, except my favorite PhantomJs (which is the fastest).
It was quite anoying, when in the debugger I could clearly see that even the parent element was set to visible. Only the prime faces component I needed to click for whatever reason (not CSS or active Styles) was not visible.
After looking at Phantom JS screenshot, I realized the window was quite tiny, and indeed the UI element was not visually visible.
You see now where I am geting at?
Phantom JS is a headless browser, it does not render you the window, but it does use a window, and the window does have a size.
The exception is quite unexpected, because the engine behind is the same as the one used for chrome and safari ... things should just work.
Well, and they did in the end.
Try to tune your driver factory like this:
WebDriver driver = new PhantomJSDriver();
driver.manage().window().setSize(new Dimension(800, 600));
return driver;
Good luck.

A Python screen capture script that can be called programmatically?

I want to open a page and grab the screen and obtain its mean color with PIL. I want to open about 100 pages so I thought a screen capture script with Python would be useful. I found several of them and I decided to use this simple one:
"""
A simple screen grabbing utility
#author Fabio Varesano -
#date 2009-03-17
"""
from PIL import ImageGrab
import time
time.sleep(5)
ImageGrab.grab().save("screen_capture.jpg", "JPEG")
But I need to run this from Command Prompt which then shows on the screen. How do I make this into a script so that I can call it programmatically? Or, what do you think is the best way to achieve this? Thanks!
You can use Selenium and any browser:
Take a screenshot with Selenium WebDriver
This is cross-platform, works on every browser.
Selenium is designed for functional web browser testing, but is suitable also for use cases like yours. More info about Python + Selenium:
http://pypi.python.org/pypi/selenium
The benefit of Selenium is that you have fine-tuned control of the browser - you can click buttons, execute Javascript, whatever you wish and the browser is no longer a black box.
This solution seems to do exactly what you want by bringing the browser to the foreground before performing the screen grab. Just replace 'firefox' with 'chrome'

Categories