Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set window size in 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"]))

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.

like image 422
David Miller Avatar asked Mar 05 '19 22:03

David Miller


People also ask

How do I change the size of the window in Selenium?

You need to use resize method to minimize the browser. void setSize() – This method is used to set the size of the current browser. Dimension getSize() – This method is used to get the size of the browser in height and width.

How to set default window size in Firefox using selenium?

It's easy. Here is the full code. from selenium import webdriver driver = webdriver.Chrome () driver.get ("Your URL") driver.set_window_size (480, 320) Make sure chrome driver is in your system path. This only happens after the window is loaded. Here is firefox profile default prefs from python selenium 2.31.0 firefox_profile.py

How to get window size in Selenium WebDriver?

Below shows the examples on how to achieve this in Selenium WebDriver C#, Ruby and Python bindings. In Ruby binding, window size can be retrieved from method driver.manage.window.size, which is a type of struct Selenium::WebDriver::Dimension defined here.

Is the browser window maximized upon launch in Selenium tests?

Note: Browser window is not maximized upon launch in any Selenium test. Changing the resolution will not maximize browser. See how to maximize and resize the browser window . A string.

What is the default screen resolution for Selenium tests?

By default, all Selenium tests on desktop run on a screen resolution of 1920x1080. You can use our custom resolution capability which lets you specify a different screen resolution for your tests. Note: Browser window is not maximized upon launch in any Selenium test. Changing the resolution will not maximize browser.


3 Answers

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:

  • python: Selenium Firefox headless returns different results
  • java: Not able to maximize Chrome Window in headless mode
like image 50
undetected Selenium Avatar answered Oct 24 '22 03:10

undetected Selenium


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")
like image 45
KunduK Avatar answered Oct 24 '22 03:10

KunduK


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.

like image 5
David Miller Avatar answered Oct 24 '22 02:10

David Miller