How to minimize the browser window in Selenium WebDriver 3

前端 未结 7 1019
南方客
南方客 2021-01-05 18:14

After maximizing the window by driver.manage().window().maximize();, how do I minimize the browser window in Selenium WebDriver with Java?

7条回答
  •  执笔经年
    2021-01-05 18:38

    Selenium's Java client doesn't have a built-in method to minimize the browsing context.

    However, as the default/common practice is to open the browser in maximized mode, while Test Execution is in progress minimizing the browser would be against the best practices as Selenium may lose the focus over the browsing context and an exception may raise during the test execution. However, Selenium's Python client does have a minimize_window() method which eventually pushes the Chrome browsing context effectively to the background.


    Sample code

    Python:

    from selenium import webdriver
    
    options = webdriver.ChromeOptions()
    options.add_argument("--start-maximized")
    options.add_experimental_option("excludeSwitches", ["enable-automation"])
    options.add_experimental_option('useAutomationExtension', False)
    driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
    driver.get('https://www.google.co.in')
    driver.minimize_window()
    

    Java:

    driver.navigate().to("https://www.google.com/");
    Point p = driver.manage().window().getPosition();
    Dimension d = driver.manage().window().getSize();
    driver.manage().window().setPosition(new Point((d.getHeight()-p.getX()), (d.getWidth()-p.getY())));
    

提交回复
热议问题