Maximize WebDriver (Selenium 2) in Python

耗尽温柔 提交于 2019-12-03 04:53:42

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 =-

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

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)

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!