Home > OS >  What is the difference between setting window size using Options and setting window size using set_w
What is the difference between setting window size using Options and setting window size using set_w

Time:03-16

I am looking for setting window size in selenium. I found this solution How to set window size in Selenium Chrome Python

rom 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()

Is there any difference in doing

options.add_argument("window-size=1400,600")

or doing

driver.set_window_size(1920, 1080)

I commented out the headless option in my windows machine and checked the code. With driver.set_window_size(1920, 1080), I see windows size changes on browser. But with options.add_argument("window-size=1920, 1080"), I don't see any change Is options.add_argument("window-size=1920, 1080") only for headless mode?

CodePudding user response:

Setting the driver window size with
driver.set_window_size(1920, 1080)
and doing that with the use of Options
options.add_argument("window-size=1920,1080")
Seems to be the same.
It's like
driver.get(url)
And
driver.navigate().to(url)
WebDriver methods that are doing exactly the same thing and actually are synonyms.
The only difference I can notice between setting the driver window size by
driver.set_window_size(1920, 1080) vs options.add_argument("window-size=1920,1080")
Is that setting / changing the driver window size by
driver.set_window_size(1920, 1080)
can be performed anywhere in your code, even several times, while setting the driver window size with
options.add_argument("window-size=1920,1080")
Can be performed only once, before creating the driver instance with
driver = webdriver.Chrome(chrome_options=options, executable_path=the_path)

CodePudding user response:

If you look at the source code:

  1. set_window_size

Code:

def set_window_size(self, width, height, windowHandle='current'):
    """
    Sets the width and height of the current window. (window.resizeTo)

    :Args:
     - width: the width in pixels to set the window to
     - height: the height in pixels to set the window to

    :Usage:
        driver.set_window_size(800,600)
    """
    if self.w3c:
        if windowHandle != 'current':
            warnings.warn("Only 'current' window is supported for W3C compatibile browsers.")
        self.set_window_rect(width=int(width), height=int(height))
    else:
        self.execute(Command.SET_WINDOW_SIZE, {
            'width': int(width),
            'height': int(height),
            'windowHandle': windowHandle})

It is explicitly mentioned that it set

width and height of the current window

takes two args:

- width: the width in pixels to set the window to
- height: the height in pixels to set the window to
  1. add_argument

Code:

def add_argument(self, argument):
    """
    Adds an argument to the list

    :Args:
     - Sets the arguments
    """
    if argument:
        self._arguments.append(argument)
    else:
        raise ValueError("argument can not be null")

Now coming to your question:

I commented out the headless option in my windows machine and checked the code. With driver.set_window_size(1920, 1080), I see windows size changes on browser. But with options.add_argument("window-size=1920, 1080"), I don't see any change Is options.add_argument("window-size=1920, 1080") only for headless mode?

With driver.set_window_size(1920, 1080), I see windows size changes on browser - this is expected.

options.add_argument("window-size=1920, 1080"), I don't see any change Is options.add_argument("window-size=1920, 1080") only for headless mode? - No, it ideally should have launched in 1920 and 1080 pixel. add_argument just add one arg to the list. doing options.add_argument("--headless") is optional.

  • Related