我尝试通过以下代码将浏览器大小设置为 (411,850),但仅针对 Windows 将其设置为 (513,850)。:
宽度为 513 而不是 411,仅适用于 Windows。
注意:我在代码中遵循此顺序
setSize(driver,411,850);
driver.get(url);
public void setSize(int width, int height) {
try {
System.out.println("Before setSize : "+driver.manage().window().getSize().width+","+driver.manage().window().getSize().height);
System.out.println("calling setSize: w=" + width + ",h=" + height);
Dimension d = new Dimension(width, height);
driver.manage().window().setSize(d);
System.out.println("After setSize : "+driver.manage().window().getSize().width+","+driver.manage().window().getSize().height);
}
catch (Exception e)
{
System.out.println("ERROR: while setSize()");
}
}
Windows 输出:
Before setSize : 1050,840
calling setSize: w=411,h=850
After setSize : 513,850
MAC 操作系统的输出:
Before setSize : 1200,1005
calling setSize: w=411,h=850
After setSize : 411,850
最佳答案
如果你想用 selenium 检查较小的浏览器窗口的大小(495, 138)。您必须使用移动选项,如下面的代码。
Map<String, Object> deviceMetrics = new HashMap<>();
deviceMetrics.put("width", 100);
deviceMetrics.put("height", 812);
deviceMetrics.put("pixelRatio", 3.0);
Map<String, Object> mobileEmulation = new HashMap<>();
mobileEmulation.put("deviceMetrics", deviceMetrics);
mobileEmulation.put("userAgent", "Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 5 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19");
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setExperimentalOption("mobileEmulation", mobileEmulation);
WebDriver driver = new ChromeDriver(chromeOptions);
关于java - SetSize() 无法正常工作 : selenium + java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55790569/