开发者

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

开发者 https://www.devze.com 2023-03-19 19:04 出处:网络
How could I start a Selenium browser (like Firefox) minimized? I want the command brows开发者_如何学运维er.start(mySettings) to start a browser minimized.I have an alternate solution that may meet you

How could I start a Selenium browser (like Firefox) minimized? I want the command brows开发者_如何学运维er.start(mySettings) to start a browser minimized.


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));


Your question does not say that why you want to run your test cases in minimized browser but unfortunately selenium do not provide any built-in function for the same.

Normally when we want to run test cases with maximized browser we use driver.manage().window().maximize();

No doubt there are several ways to minimize your window through code by using Java key event by using keyboard shortcuts for minimimzing window or by using JavaScriptExecuter but that too depend on which OS and language you are working.

One more thing you can try is HtmlUnitDriver.By using this you cant even see the browser, so that may also serve your purpose if you have a case of not opening the browser while execution of test cases.


Dimension windowMinSize = new Dimension(100,100);
driver.manage().window().setSize(windowMinSize);


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

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

Screenshot:

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


Without knowing your motive for minimizing the browser and assuming that you are using the WebDriver drivers (Selenium v2) and don't want a UI to pop up, one could use the lightweight browser HtmlUnitDriver.


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.


When Using Python to Move the FireFox Browser off screen:

driver = webdriver.FireFox()
driver.set_window_position(-2000,0)


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()


The workarounds mentioned in the post did not work for NodeWebKit browser, so as a workaround i had to use native C# code as mentioned below:

public static void MinimiseNWKBrowser(IWebDriver d)
{
    var body = UICommon.GetElement(By.TagName("body"), d);
    body.Click();
    string alt = "%";
    string space = " ";
    string down = "{DOWN}";
    string enter = "{ENTER}";
    SendKeys.SendWait(alt + space);
    for(var i = 1; i <= 5; i++)
    {
        SendKeys.SendWait(down);
    }
    SendKeys.SendWait(enter);            
}

So this workaround basically uses "ALT+SPACE" to bring up the browser action menu to select "MINIMIZE" from the options and presses "ENTER"


In php we can use JavaScript command to minimize the browser window.

$this->selenium->getEval("Minimize();");

and similar command for java :

browser.getEval("Minimize();");


The best way to minimize your browser is to use shortcut using Robot class.

Shortcut: Alt+Space+N (for Windows)

Robot robot=new Robot();
robot.keyPress(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_SPACE);
robot.keyPress(KeyEvent.VK_N);
robot.keyRelease(KeyEvent.VK_ALT);
robot.keyRelease(KeyEvent.VK_SPACE);
robot.keyRelease(KeyEvent.VK_N);

By using above code you can minimize your browser.


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())));
    


You can use:

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

For example code snippet with chrome driver:

System.setProperty("webdriver.chrome.driver", "C://chromedriver.exe");
driver = new ChromeDriver();
baseUrl = "chrome://newtab/";
driver.manage().window().maximize().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

for Firefox use just "firefordriver.exe"


driver.manage().window().minimize();

This should help minimize the window. You can also use "maximize" in place of "minimize" to maximize the window.

0

精彩评论

暂无评论...
验证码 换一张
取 消