简体   繁体   English

如何在 Selenium Chrome Python 中设置窗口大小

[英]How to set window size in Selenium Chrome Python

The following code to resize a selenium chrome window does not work:以下用于调整 selenium chrome 窗口大小的代码不起作用:

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"]))

From which the output is:输出是:

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")

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

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.我想让 chrome 驱动程序工作,因为 Firefox 不在后台运行(我需要),而且 ie 很痛苦。

I want to figure this out because I can't see an element I need to click when the window is so small.我想弄清楚这一点,因为当窗口很小时,我看不到需要单击的元素。

A bit unclear why and exactly where you are stuck.有点不清楚为什么以及您被卡住的确切位置。 Possibly the extra .可能是额外的. as in height = {}px.height = {}px. is creating the chaos.正在制造混乱。 Perhaps along with -headless argument I am able to set/retrieve the Chrome browser Window Size as follows:也许与-headless参数一起,我可以设置/检索Chrome浏览器窗口大小,如下所示:

  • 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

tl; tl; dr博士

You find a couple of relevant discussion on window size in:您可以在以下位置找到一些关于窗口大小的相关讨论:

Instead of代替

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

Can you please try this.你能试试这个吗。

options.add_argument('window-size=1920x1080');

OR或者

options.add_argument("--start-maximized")

So I finally figured out what the problem is: Running Windows task scheduler with option 'run whether user is logged on or not' only opens a small browser (1024x768) that CANNOT be resized, even with all the great suggestions being offered here.所以我终于找出了问题所在:运行带有“无论用户是否登录都运行”选项的 Windows 任务调度程序只会打开一个无法调整大小的小浏览器 (1024x768),即使这里提供了所有很棒的建议。

See the same issue resolved here: screen resolution in mode "Run whether user is logged on or not", in windows task scheduler请参阅此处解决的相同问题: windows 任务计划程序中“无论用户是否登录都运行”模式下的屏幕分辨率

So the less than ideal workaround is to only run when user is logged on.因此不太理想的解决方法是仅在用户登录时运行。

Thanks for all your help!感谢你的帮助!

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

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