如何在 Selenium 中设置浏览器窗口大小?
我们可以通过以下方法设置浏览器窗口的大小 -
getSize() 方法
Javascript 执行器
示例
用 setSize() 方法。
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
import java.util.List;
public class BrowserDimension {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
WebDriver driver = new ChromeDriver();
String url = "https://tutorialspoint.com/index.htm";
driver.get(url);
// maximize the browser
driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS);
// fetching the current window size with getSize()
System.out.println(driver.manage().window().getSize());
//Create object of Dimensions class
Dimension dm = new Dimension(450,630);
//Setting the current window to that dimension
driver.manage().window().setSize(dm);
driver.close();
}
}示例
用 Javascript 执行器。
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.JavascriptExecutor;
public class BrowserDimensionJS {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
WebDriver driver = new ChromeDriver();
String url = "https://tutorialspoint.com/index.htm";
driver.get(url);
// maximize the browser
driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS);
JavascriptExecutor js = (JavascriptExecutor) driver;
// set size with window.resizeTo() method
js.executeScript("window.resizeTo(450,630);");
driver.close();
}
}
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP