How to resize the window of Chrome and Firefox with Selenium in Python?
Asked Answered
V

3

15

We created extensions for Chrome, Firefox and Safari and we want to test our extensions with Selenium. We have an Ubuntu server and I installed Firefox and Chrome in it (with ChromeDriver). The tests work, but the problem is that the Chrome window is too small, and many tests fail in Chrome because of the size of the window. With Firefox the window is fine and the tests don't fail. How do I make the window size bigger? We want the window size to be 1920x1080. Here is our code:

run_daily_selenium_tests.sh:

#!/bin/bash

cd /home/ubuntu/selenium_tests

sudo Xvfb :99 -ac -screen 0 1920x1080x24 >/dev/null 2>&1 &
export DISPLAY=:99

today_date_hour=`TZ='Asia/Tel_Aviv' date +"%Y-%m-%d_%H"`
start_hour=`TZ='Asia/Tel_Aviv' date +"%H"`
selenium_test_prefix="/home/ubuntu/selenium_tests/tests/"
selenium_test_suffix=".py"
log_prefix="/home/ubuntu/logs/selenium_tests/"

run_selenium_test()
{
    selenium_test_file_name="${selenium_test_prefix}${file_name}${selenium_test_suffix}"
    log_suffix="_${file_name}.log"
    log_file_name="${log_prefix}${today_date_hour}${log_suffix}"
    python "$selenium_test_file_name" >"$log_file_name" 2>&1
    cat "$log_file_name" | mail -s "$test_name - `TZ='Asia/Tel_Aviv' date +"%Y-%m-%d %H:%M:%S"`" <my_email_address>
}

file_name="chrome_inbox_without_extension_test"
test_name="Chrome Inbox Without Extension Test"
run_selenium_test

file_name="chrome_inbox_1_with_extension_test"
test_name="Chrome Inbox 1 With Extension Test"
run_selenium_test

.....

sudo killall Xvfb >/dev/null 2>&1

if [ $start_hour = "08" ]; then
    cd /home/ubuntu
    /home/ubuntu/scripts/send_daily_report_html.sh
fi

Selenium tests code:

def get_default_caps(self):
    caps = {}
    caps['name'] = 'Base Selenium Test'
    caps['build'] = '1.0.1'
    if (self.browser == "chrome"):
        self.browser_version = '41.0.2272.118'
        caps['browser'] = 'Chrome'
    elif (self.browser == "firefox"):
        self.browser_version = '37.0.1'
        caps['browser'] = 'Firefox'
    elif (self.browser == "safari"):
        caps['browser'] = 'Safari'
        caps['os'] = 'OS X'
        self.browser_version = "{}.0".format(randint(7, 8))
        if (self.browser_version == "7.0"):
            caps['os_version'] = 'Mavericks'
        elif (self.browser_version == "8.0"):
            caps['os_version'] = 'Yosemite'
    caps['browser_version'] = self.browser_version
    caps['resolution'] = '1920x1080'
    caps['browserstack.debug'] = 'true'
    return caps

def start_selenium_webdriver(self, caps, chrome_options=None, firefox_profile=None):
    print("Starting test \"{}\" with {} {}, resolution {}.".format(caps['name'], caps['browser'], caps['browser_version'], caps['resolution']))
    if (self.browser == "chrome"):
        self.driver = webdriver.Chrome(chrome_options=chrome_options)
    elif (self.browser == "firefox"):
        self.driver = webdriver.Firefox(firefox_profile=firefox_profile)
    else:
        self.driver = webdriver.Remote(
            command_executor='http://username:[email protected]:80/wd/hub',
            desired_capabilities=caps
        )
    self.driver.implicitly_wait(time_to_wait=5)
    self.driver.maximize_window()

Firefox window: Firefox window Chrome window: Chrome window

Solution:

I removed self.driver.maximize_window() and instead I added the following lines:

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

The results:

With Chrome: Window size: width = 1919px, height = 1079px.

With Firefox: Window size: width = 1920px, height = 1080px.

The tests pass now.

Volleyball answered 13/4, 2015 at 7:37 Comment(0)
D
28

For Chromejust add before "webdriver.Chrome" init:

chrome_options.add_argument("--window-size=1920x1080")

For all browsers:

There is an official selenium python binding for that: http://selenium.googlecode.com/git/docs/api/py/webdriver_remote/selenium.webdriver.remote.webdriver.html#selenium.webdriver.remote.webdriver.WebDriver.set_window_size

driver.set_window_size(1920, 1080)

or

driver.maximize_window()

or with javascript

driver.execute_script("window.resizeTo(1920,1080)")
Deary answered 13/4, 2015 at 8:3 Comment(5)
I already did driver.maximize_window() right after running Chrome or Firefox (look at my code), do I have to do anything else? I also think the window in Firefox is not 1920x1080, although it's big enough for the tests not to fail.Volleyball
No, you needn't. Are you sure they are not maximized fully? Have tou tried to take the actual size of the window (print driver.get_window_size())?Deary
Thanks, I checked with driver.maximize_window(): Chrome {u'width': 945, u'height': 1060}, test failed, Firefox {u'width': 1440, u'height': 782}, test passed. With driver.set_window_size(1920, 1080): Chrome {u'width': 1919, u'height': 1079}, test passed, Firefox {u'width': 1920, u'height': 1080}, test passed. So this solved me problem. Thank you!Volleyball
I would suggest you not to use maximize_window() but to use specific set_window_size(x, y). Eventually, different hosts got different max settings, and it can lead to inconsistency.Ulcerous
Note that set_window_size() doesn't work in headless mode, which can lead to weird exceptions like ElementNotVisibleException.Gamber
S
0
driver.set_window_size(width, height) 

If you don't know the width and height use the How Big Is My Browser website.

Seductress answered 23/8, 2020 at 0:37 Comment(0)
C
0

Chrome. *-window-size and window-size also work:

from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_argument("--window-size=1920,1080")
driver = webdriver.Chrome(options=options)

Microsoft Edge. *-window-size and window-size also work:

from selenium import webdriver

options = webdriver.EdgeOptions()
options.add_argument("--window-size=1920,1080")
driver = webdriver.Edge(options=options)

Firefox. *-width and -height also work while width and height don't work:

from selenium import webdriver

options = webdriver.FirefoxOptions()
options.add_argument("--width=1920")
options.add_argument("--height=1080")
driver = webdriver.Firefox(options=options)

*My answer explains it more.

Conrado answered 12/9, 2023 at 5:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.