How to set window size for Chrome with Selenium in Python?
Asked Answered
R

5

24

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

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

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

Ryley answered 5/3, 2019 at 22:40 Comment(3)
Why not do a scroll to ?Orthodoxy
I've tried this: driver.execute_script("arguments[0].scrollIntoView();", button2) where button2 is the hidden button at the bottom of a menu drop down list, and that does not work either.Ryley
Are you clicking the dropdown to open it before trying to click the desired element? The window should scroll for you once the element is visible. If that doesn't work, you can always click the element using JS. It's not a user scenario (because users can't click things they can't see) but if you aren't trying to automate a user scenario or are really stuck with no other options, that should work.Oblate
D
34

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
    

tl; dr

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

Doggoned answered 6/3, 2019 at 5:45 Comment(0)
G
8

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")
Guimar answered 5/3, 2019 at 23:8 Comment(1)
I have already tried the first and third options with no luck. I just tried the middle option and it doesn't work either. My window is still too small, and I can't see the element I need to click. Curious why this is so complicated / not working for me. Thanks for your help.Ryley
O
7

You can use set_window_size function in webdriver class:

driver.set_window_size(width, height)

for ex:

driver.set_window_size(800, 600)
Ossiferous answered 4/2, 2023 at 20:38 Comment(0)
R
6

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.

See the same issue resolved here: screen resolution in mode "Run whether user is logged on or not", in windows task scheduler

So the less than ideal workaround is to only run when user is logged on.

Ryley answered 6/3, 2019 at 14:14 Comment(0)
B
0

You can set window size for Chrome as shown below. *-window-size and window-size also work:

from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_argument("--window-size=1024,768")
driver = webdriver.Chrome(options=options)

And, you can also set window size for Chrome with the code below according to the doc:

from selenium import webdriver

driver = webdriver.Chrome()
driver.set_window_size(1024, 768)

*My answer explains it more.

Bespatter answered 12/9, 2023 at 6:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.