简体   繁体   English

在 Python 中最大化 WebDriver (Selenium 2)

[英]Maximize WebDriver (Selenium 2) in Python

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.我正在尝试编写一个简单的脚本来检查我是否有任何标记为 SOMETHING 的 gmail 电子邮件,然后打开一个 firefox 浏览器窗口到登录页面,然后转到其他内容。

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.在其他地方,我读到有一个 windowMaximize() 或 window_maximize(),但无法让它们工作,因为我尝试过的每个配置都声称它不存在于任何模块。

I only know a little python, and am working in Mac OSX我只懂一点 python,在 Mac OSX 上工作

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.这似乎工作正常 - 除非我使用 Chrome。 I'm not sure if this is a defect, as it works flawlessly in IE9 and Firefox.我不确定这是否是一个缺陷,因为它在 IE9 和 Firefox 中完美运行。

edit: This is a feature which has yet to be implemented in Chromedriver -= Link to issue =-编辑:这是一项尚未在 Chromedriver 中实现的功能-= 发布链接 =-

edit (8 years later): Apparently this is working in Chrome on Linux and Windows - so, yay!编辑(8 年后):显然这是在 Linux 和 Windows 上的 Chrome 中工作的 - 所以,是的! I haven't tested it, but I am optimistic since it has been nearly a decade since the original answer was provided.我还没有测试过,但我很乐观,因为距离提供原始答案已经快十年了。

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您还可以使用此库来最大化窗口等,请参阅文档: https : //seleniumhq.github.io/selenium/docs/api/rb/Selenium/WebDriver/Chrome/Options.html

对于 Chrome,应该在包含修订的下一个版本中,实现: http : //code.google.com/p/chromedriver/issues/detail?id=65

I solve it with this line我用这条线解决了

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM