使用选项设置 window 大小和使用 set_window_size 方法设置 window 大小有什么区别

What is the difference between setting window size using Options and setting window size using set_window_size method

我正在寻找在 selenium 中设置 window 大小。 我找到了这个解决方案 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()

做起来有什么区别吗

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

或正在做

driver.set_window_size(1920, 1080)

我在我的 windows 机器上注释掉了 headless 选项并检查了代码。 使用 driver.set_window_size(1920, 1080),我在浏览器上看到 windows 大小变化。 但是对于 options.add_argument("window-size=1920, 1080"),我没有看到任何变化 options.add_argument("window-size=1920, 1080") 仅适用于无头模式吗?

设置驱动程序 window 大小
driver.set_window_size(1920, 1080)
并使用 Options
options.add_argument("window-size=1920,1080")
好像是一样的。
就像
driver.get(url)
并且
driver.navigate().to(url)
WebDriver 正在执行 exactly the same thing 并且实际上是同义词的方法。
我注意到将驱动程序 window 大小设置为
之间的唯一区别 driver.set_window_size(1920, 1080) 对比 options.add_argument("window-size=1920,1080")
是将驱动程序 window 大小设置/更改
driver.set_window_size(1920, 1080)
可以在您的代码中的任何地方执行,甚至多次,同时设置驱动程序 window 大小
options.add_argument("window-size=1920,1080")
只能执行一次,在使用
创建驱动程序实例之前 driver = webdriver.Chrome(chrome_options=options, executable_path=the_path)

如果你查看源代码:

  1. set_window_size

代码:

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

明确提到它设置

width and height of the current window

接受两个参数:

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

代码:

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

现在回答你的问题:

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?

driver.set_window_size(1920, 1080),我看到 windows 尺寸 浏览器上的更改 - 这是预期的。

options.add_argument("window-size=1920, 1080"),我没有看到任何变化是 options.add_argument("window-size=1920, 1080") 仅适用于无头模式? - 不,理想情况下,它应该在 1920 和 1080 像素上推出。 add_argument 只需将一个 arg 添加到 列表 。做 options.add_argument("--headless") 是可选的。