如何使用FirefoxProfile或FirefoxOptions通过Selenium设置Firefox浏览器的窗口位置



我需要通过使用创建驱动程序来更改Firefox窗口的位置

driver = webdriver.Firefox()

我知道在创建驱动程序后可以更改窗口位置:

driver.set_window_position()

我找不到如何使用Firefox配置文件或选项:

profile = webdriver.FirefoxProfile()
profile.set_preference("some_preference", my_preference)

options = Options()
options.some_optins = my_options

最后:

driver = Webdriver.Firefox(firefox_profile=profile, options=options) 

你看对了。

设置窗口位置()

set_window_position()设置当前窗口的xy位置。

  • 实现:

    set_window_position(x, y, windowHandle='current')
    Sets the x,y position of the current window. (window.moveTo)
    Arguments :
    x: the x-coordinate in pixels to set the window position
    y: the y-coordinate in pixels to set the window position
    Usage :
    driver.set_window_position(0,0)
    
  • 定义:

    def set_window_position(self, x, y, windowHandle='current'):
    if self.w3c:
    if windowHandle != 'current':
    warnings.warn("Only 'current' window is supported for W3C compatibile browsers.")
    return self.set_window_rect(x=int(x), y=int(y))
    else:
    self.execute(Command.SET_WINDOW_POSITION,
    {
    'x': int(x),
    'y': int(y),
    'windowHandle': windowHandle
    })
    

总之,window_position耦合到属于浏览器的窗口句柄,并且只能由webdriver实例处理。

此功能也无法通过以下方式处理:

  • firefox_profile->set_preference(key, value):设置配置文件中所需的首选项
  • firefox.options->set_preference(name, value):设置首选项

相关内容

最新更新