How to set browser viewport size

后端 未结 3 2131
野趣味
野趣味 2021-02-20 13:29

I\'m trying to create a cross-browser Python-Selenium test script. So I need all results to be same no matter which webdriver (Chrome or <

3条回答
  •  闹比i
    闹比i (楼主)
    2021-02-20 13:50

    Here is function to set the viewport size:

    def set_viewport_size(driver, width, height):
        window_size = driver.execute_script("""
            return [window.outerWidth - window.innerWidth + arguments[0],
              window.outerHeight - window.innerHeight + arguments[1]];
            """, width, height)
        driver.set_window_size(*window_size)
    

    Usage :

    from selenium import webdriver
    
    driver = webdriver.Chrome()
    
    # set the viewport size to 800 x 600
    set_viewport_size(driver, 800, 600)
    
    # display the viewport size
    print driver.execute_script("return [window.innerWidth, window.innerHeight];")
    

提交回复
热议问题