如何在Selenium Chrome Python中设置窗​​口大小 [英] How to set window size in Selenium Chrome Python

查看:188
本文介绍了如何在Selenium Chrome Python中设置窗​​口大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下用于调整硒铬窗口大小的代码不起作用:

The following code to resize a selenium chrome window does not work:

driver.set_window_size(1920, 1080)
time.sleep(5)
size = driver.get_window_size()
print("Window size: width = {}px, height = {}px.".format(size["width"], size["height"]))

输出的来源是:

Window size: width = 1044px, height = 788px

我也尝试过使用选项来设置驱动程序创建时的窗口大小(还有很多其他事情,在下面似乎是注释),但也无法使其起作用:

I've also tried using options to set the window size on driver creation (and lots of other things, seem comments below), but can't get it to work either:

options.add_argument("--window-size=1920,1080")

我正在使用Selenium 3.14.0,chrome驱动程序版本72.0.3626.109并在后台/无头模式下运行:我确实需要在后台运行我的代码,这意味着它会在后台自动启动.我认为无头和背景之间有微妙的区别,无头与启动时是与特定用户相关联的,背景也无头,但可能与特定用户无关,并且可能具有其他特质-我开始认为这可能成为我的问题的一部分.

I am using selenium 3.14.0, chrome driver version 72.0.3626.109 and running in background / headless mode: I am literally needing to run my code in background, meaning it launches automatically in the background. I think there is a subtle difference between headless, which when launched is associated with a particular user, and background, which is also headless but may not be associated with a particular user and may have other idiosyncrasies - I'm starting to think this may be part of my issue.

我想让chrome驱动程序正常工作,因为firefox不在后台运行(我需要),也就是说很痛苦.

I'd like to get chrome driver to work because firefox does not run in the background (which I need), and ie is a pain.

我想弄清楚这一点,因为当窗口很小时,我看不到需要单击的元素.

I want to figure this out because I can't see an element I need to click when the window is so small.

推荐答案

有点不清楚为什么以及被困在何处.可能与height = {}px.中一样的 . 造成了混乱.也许与-headless参数一起,我可以如下设置/获取 Chrome 浏览器 Window Size :

A bit unclear why and exactly where you are stuck. Possibly the extra . as in height = {}px. is creating the chaos. Perhaps along with -headless argument I am able to set/retrieve the Chrome browser Window Size as follows:

  • 代码块:

  • Code Block:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
options.add_argument("--headless")
options.add_argument("window-size=1400,600")
driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe', service_args=["--log-path=./Logs/DubiousDan.log"])
driver.get("http://google.com/")
print ("Headless Chrome Initialized")
print(driver.get_window_size())
driver.set_window_size(1920, 1080)
size = driver.get_window_size()
print("Window size: width = {}px, height = {}px".format(size["width"], size["height"]))
driver.quit()

  • 控制台输出:

  • Console Output:

    Headless Chrome Initialized
    {'width': 1400, 'height': 600}
    Window size: width = 1920px, height = 1080px
    

  • 您可以在以下位置找到有关 窗口大小 的一些相关讨论:

    You find a couple of relevant discussion on window size in:

    • python: Selenium Firefox headless returns different results
    • java: Not able to maximize Chrome Window in headless mode

    这篇关于如何在Selenium Chrome Python中设置窗​​口大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆