【问题标题】:SetSize() not working properly : selenium + javaSetSize() 无法正常工作:selenium + java
【发布时间】:2019-04-22 07:13:40
【问题描述】:

我正在尝试通过以下代码设置我的浏览器大小,方法是将其设置为 (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-OS 的输出:

Before setSize : 1200,1005
calling setSize: w=411,h=850
After setSize : 411,850

【问题讨论】:

  • 什么不正常是最大化还是不正常,请描述它正常不工作不说什么,否则你可以直接使用这个driver.manage().window().maximize();对于全窗口..
  • @akshaypatil 我正在尝试通过上面的代码设置我的浏览器大小,方法是将其设置为 (411,850),但仅针对 Windows 将其设置为 (513,850)
  • 它在我的电脑上运行良好,可能是你发送了错误的参数
  • @iamsankalp89 我按这个顺序做setSize(driver,411,850); driver.get(url);
  • 在 get(url) 函数之前调用 setSize。你是按照同样的顺序吗?

标签: java selenium google-chrome selenium-chromedriver


【解决方案1】:

如果你想用 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);

【讨论】:

  • 或者你可以尝试打开新窗口并通过 javascript window.open('url', '','width=100,height=200'); window.resizeTo(300, 450)
【解决方案2】:

这是因为 chrome 有最小窗口大小限制。 Selenium 不能使窗口小于那个值。如果您尝试手动调整 pc 中的 chrome 大小,您可能会发现它的缩放比例不会低于某个限制。您将窗口的宽度和高度设置为低于 chrome 允许的限制。这就是为什么你会得到不同的结果。

我认为它是根据屏幕尺寸计算的。这就是为什么每个人都可以调整到您帖子中的给定点并且无法重现该问题的原因。

在我的电脑中(495,138) 是最小限制。

【讨论】:

    猜你喜欢
    • 2020-05-04
    • 1970-01-01
    • 2016-08-01
    • 1970-01-01
    • 2020-03-18
    • 1970-01-01
    • 1970-01-01
    • 2018-07-02
    • 1970-01-01
    相关资源
    最近更新 更多