如何使用 C# 在 Selenium WebDriver (Selenium 2) 中最大化浏览器窗口?

如何使用 C# 在 Selenium WebDriver (Selenium 2) 中最大化浏览器窗口?

问题描述:

有没有什么办法可以在 C# 中使用 WebDriver (Selenium 2) 来最大化浏览器窗口?

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

将此功能添加到 WebDriver 有一个悬而未决的问题,可在此处跟踪:http://code.google.com/p/selenium/issues/detail?id=174

There's an outstanding issue to add this functionality to WebDriver, which can be tracked here: http://code.google.com/p/selenium/issues/detail?id=174

一种解决方法是使用 JavascriptExector 如下:

A workaround would be to use the JavascriptExector as follows:

public void resizeTest() {
    driver.Navigate().GoToUrl("http://www.example.com/");
((IJavaScriptExecutor)driver).ExecuteScript("window.resizeTo(1024, 768);");
}