How could I start a Selenium browser(like Firefox) minimized?

前端 未结 14 1367
野的像风
野的像风 2020-12-15 04:35

How could I start a Selenium browser (like Firefox) minimized? I want the command browser.start(mySettings) to start a browser minimized.

相关标签:
14条回答
  • 2020-12-15 05:20

    Selenium doesn't have a built-in method to minimize the Browsing Context. Minimizing the browser while the Test Execution is In Progress would be against the best practices as Selenium may loose the focus over the Browsing Context and an exception may raise during the Test Execution. You can find a relevant detailed discussion in How to execute tests with selenium webdriver while browser is minimized


    However, to mimic the functionality of minimizing the Browsing Context you can use the following steps:

    • Set the dimension of the Browsing Context to [0, 0]
    • Set the position of the top left corner of the Browsing Context just below the Viewport
    • Code Block (Java):

      driver.navigate().to("https://www.google.com/");
      Point p = driver.manage().window().getPosition();
      Dimension d = driver.manage().window().getSize();
      driver.manage().window().setSize(new Dimension(0,0));
      driver.manage().window().setPosition(new Point((d.getHeight()-p.getX()), (d.getWidth()-p.getY())));
      
    0 讨论(0)
  • 2020-12-15 05:20

    After you define the driver

    driver = webdriver.Chrome(r"FULL PATH TO YOUR CHROMEDRIVER")
    

    Do

    driver.minimize_window()
    

    And then call the site

    driver.get(r'SITE YOU WANT TO SELECT')
    

    It will minimize.

    0 讨论(0)
  • 2020-12-15 05:22

    When Using Python to Move the FireFox Browser off screen:

    driver = webdriver.FireFox()
    driver.set_window_position(-2000,0)
    
    0 讨论(0)
  • 2020-12-15 05:24

    I have an alternate solution that may meet your needs. You can set the position of the WebDriver to be outside of your view. That way, it'll be out of sight while it runs. (It may start at a visible location, but it will only be there for an instant.)

    FirefoxDriver driver = new FirefoxDriver();
    driver.manage().window().setPosition(new Point(-2000, 0));
    
    0 讨论(0)
  • 2020-12-15 05:27
    driver.set_window_position(2000,2000)
    

    or

    (x,y) values more than monitor resolution

    Later if u want to see the window again

    driver.maximize_window()
    
    0 讨论(0)
  • 2020-12-15 05:28

    For C# you can minimize window easily, also with a built in way.

    See: https://www.selenium.dev/documentation/en/webdriver/browser_manipulation

    Screenshot:

    screenshot

    0 讨论(0)
提交回复
热议问题