25

I'm attempting to write a simple script that checks if I have any gmail emails labeled SOMETHING and then opens a firefox browser window to a login page, after which it goes to something else.

Here's what I'm doing:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.action_chains import ActionChains
import time, imaplib

Eusername = "someone@gmail.com"
Epassword = "password1"

username = "username"
password = "password2"

imaps = imaplib.IMAP4_SSL('imap.gmail.com','993')
imaps.login(Eusername,Epassword)

imaps.select('SOMETHING')
status, response = imaps.status('SOMETHING', "(UNSEEN)")
unreadcount = int(response[0].split()[2].strip(').,]'))

while unreadcount > 0:
    driver = webdriver.Firefox()
    driver.get('http://wwww.SomeURL.com/some_login.html')
    time.sleep(3)
    inputElement = driver.find_element_by_name("user")
    inputElement.send_keys(username)
    inputElement = driver.find_element_by_name("pw")
    inputElement.send_keys(password)
    inputElement.submit()
    time.sleep(1)
    driver.get('http://www.SomeURL.com/somethingelse.html')
    imaps.select('SOMETHING')
    typ ,data = imaps.search(None,'UnSeen')
    imaps.store(data[0].replace(' ',','),'+FLAGS','\Seen')

I've spent hours search and haven't found a solution to maximize the browser window. Elsewhere i've read that there is a windowMaximize() or window_maximize(), but have not been able to get them to work since every configuration I've tried claims it doesn't exist for whatever module.

I only know a little python, and am working in Mac OSX

Ripon Al Wasim
  • 36,924
  • 42
  • 155
  • 176
Dicky Brucks
  • 251
  • 1
  • 3
  • 3
  • Its very easy , you just have to write this line. driver.maximize_window() For more details with example please refer this url, I have already answered here also: http://edu.yoursfriends.com/839/maximize-window-in-selenium-webdriver-using-python – Himanshu Tewari Feb 01 '16 at 05:09

5 Answers5

64

I've never used this functionality before, so I tried it out.

driver.maximize_window()

This seems to work fine - unless I am using Chrome. I'm not sure if this is a defect, as it works flawlessly in IE9 and Firefox.

edit: This is a feature which has yet to be implemented in Chromedriver -= Link to issue =-

edit (8 years later): Apparently this is working in Chrome on Linux and Windows - so, yay! I haven't tested it, but I am optimistic since it has been nearly a decade since the original answer was provided.

Isaac
  • 9,372
  • 4
  • 28
  • 31
  • 3
    get the newest version of chrome driver and this works on windows – HelloW Sep 12 '13 at 17:46
  • 1
    Another approach is to get screen resolution from system **with** `xdpyinfo | grep 'dimensions:'` **or** `xrandr | grep '*'` **or** `inxi -G | egrep 'Resolution:.*hz' -o` **then** set it with `driver.set_window_size("width", "height")` – m3nda Oct 26 '15 at 11:22
  • 3
    The way to do it with Chrome is: `options = webdriver.ChromeOptions()` `options.add_argument("--start-maximized")` `driver = Chrome(chrome_options=options)` – HenryM Dec 19 '16 at 10:56
  • 1
    please update your answer, the feature is now working in chrome for both Linux and windows. – Deepam Gupta Nov 29 '20 at 19:26
4

Even if this is old, it's cool to know that you can always get values from system then set it by hand. This would work on every webdriver you use.

#!/usr/bin/env python
#! -*- coding: utf-8 -*-

import selenium
from selenium import webdriver

import os, sys, time

import wx

print "example with maximize_window()"
nav = webdriver.Firefox()
nav.maximize_window()
time.sleep(3)
nav.quit()

print 'example with fixed set_window_size("1024", "768")'
nav = webdriver.Firefox()
nav.set_window_size("1024", "768")
time.sleep(3)
nav.quit()

print "example grabbing size with wx (wxWidgets)"
nav = webdriver.Firefox()
app = wx.App(False) #wx.App(False) # the wx.App object must be created first.
screenxy = wx.GetDisplaySize()  # returns a tuple
nav.set_window_size(screenxy[0], screenxy[1])
time.sleep(3)
nav.quit()


sys.exit(0)
m3nda
  • 1,986
  • 3
  • 32
  • 45
3

There is a really simple way to create a maximized window:

from selenium.webdriver.chrome.options import Options

chrome_options = Options()
# maximized window
chrome_options.add_argument("--start-maximized")

You can also use this library for maximizing the window and more, see the documentation: https://seleniumhq.github.io/selenium/docs/api/rb/Selenium/WebDriver/Chrome/Options.html

Gavriel Cohen
  • 4,355
  • 34
  • 39
1

For Chrome, should be in the next build that includes the revision, implemented: http://code.google.com/p/chromedriver/issues/detail?id=65

zvezda
  • 1,223
  • 9
  • 16
0

I solve it with this line

self.driver = webdriver.Chrome()
self.driver.maximize_window()