How to set browser viewport size

后端 未结 3 2134
野趣味
野趣味 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条回答
  •  遥遥无期
    2021-02-20 14:12

    Here's the Java version for @Florent B. answer :

            int width = "500";
            int height = "500";
    
            //Remove the window from fullscreen (optional), if it s in fullscreen the outerHeight is not accurate
            browser.manage().window().setSize(new Dimension(800,800));
    
            JavascriptExecutor js= (JavascriptExecutor)browser;
    
            String windowSize = js.executeScript("return (window.outerWidth - window.innerWidth + "+width+") + ',' + (window.outerHeight - window.innerHeight + "+height+"); ").toString();
    
            //Get the values
            width = Integer.parseInt(windowSize.split(",")[0]);
            height = Integer.parseInt(windowSize.split(",")[1]);
    
            //Set the window
            browser.manage().window().setSize(new Dimension(width, height));
    

提交回复
热议问题