How to maximize the browser window in Selenium WebDriver (Selenium 2) using C#?

前端 未结 28 2026
悲&欢浪女
悲&欢浪女 2020-11-29 18:20

Is there any way to maximize the browser window using WebDriver (Selenium 2) with C#?

相关标签:
28条回答
  • 2020-11-29 18:21

    Java

    driver.manage().window().maximize();
    

    Python

    driver.maximize_window()
    

    Ruby

    @driver.manage.window.maximize
    

    OR

    max_width, max_height = driver.execute_script("return [window.screen.availWidth, window.screen.availHeight];")
    @driver.manage.window.resize_to(max_width, max_height)
    

    OR

    target_size = Selenium::WebDriver::Dimension.new(1600, 1268)
    @driver.manage.window.size = target_size
    
    0 讨论(0)
  • 2020-11-29 18:21

    If you are using the Chrome Driver you can set the capabilities

        var capabilities = new DesiredCapabilities();
    
        var switches = new List<string>
                           {
                               "--start-maximized"
                           };
    
        capabilities.SetCapability("chrome.switches", switches);
    
        new ChromeDriver(chromedriver_path, capabilities);
    
    0 讨论(0)
  • 2020-11-29 18:21

    There is a function that you can use to maximize the window in Python which is window_maximize(). And this is how I'm using it.Hope this helps -

    from selenium import selenium
    sel = selenium('localhost', 4444, '*firefox', 'http://10.77.21.67/')
    sel.start()
    sel.open('/')
    sel.wait_for_page_to_load(60000)
    #sel.window_focus()
    sel.window_maximize()
    png = sel.capture_screenshot_to_string()
    f = open('screenshot.png', 'wb')
    f.write(png.decode('base64'))
    f.close()
    sel.stop()
    
    0 讨论(0)
  • 2020-11-29 18:23

    You can use Selenium Emulation in WebDriver:

    selenium = new WebDriverBackedSelenium(driver,url);
    selenium.windowMaximize();
    
    0 讨论(0)
  • 2020-11-29 18:23

    I used this solution

                OpenQA.Selenium.Chrome.ChromeOptions chromeoptions = new OpenQA.Selenium.Chrome.ChromeOptions();
                chromeoptions.AddArgument("--start-maximized");
                OpenQA.Selenium.Chrome.ChromeDriver chrome = new OpenQA.Selenium.Chrome.ChromeDriver(chromeoptions);
    
    0 讨论(0)
  • 2020-11-29 18:26

    The following Selenium Java code snippet worked for me:

    driver.manage().window().setPosition(new Point(0,0));
    java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
    Dimension dim = new Dimension((int) screenSize.getWidth(), (int) screenSize.getHeight());
    driver.manage().window().setSize(dim);
    
    0 讨论(0)
提交回复
热议问题