【问题标题】:What is the unit of selenium's Dimension(1200,600)?selenium的Dimension(1200,600)的单位是什么?
【发布时间】:2018-08-20 12:30:42
【问题描述】:

我正在自动化一个应用程序,该应用程序需要具有 1200x600 像素大小的浏览器。我正在使用如下代码来最大化(我不想使用dirver.manage().maximize() 代码,它对应用程序有一些问题)。 下面是我的代码

Dimension d = new Dimension(1200,600);
//Resize the current window to the given dimension
driver.manage().window().setSize(d);

但是,该应用程序仍然与此尺寸不兼容。那么有人能说出在 Dimention() 构造函数中这个 1200 和 600 使用的单位是什么吗? 如果 some 在 Dimenstion(xxx,xxx) 中给出等效值 1200x600 像素值就可以了。

注意:使用 google-chrome 65.x,windows 10。

【问题讨论】:

  • 该命令以像素为单位设置外部窗口的大小,而不是包含网页的内部窗口/视口的大小。另请注意,windows 将窗口的大小限制为桌面的大小。
  • System.setProperty("webdriver.chrome.driver";, "/path/to/chromedriver"); ChromeOptions 选项 = 新的 ChromeOptions(); options.addArguments("窗口大小=800,480"); DesiredCapabilities cap = DesiredCapabilities.chrome(); cap.setCapability(ChromeOptions.CAPABILITY,选项);试试这个。
  • 有什么需要导入的吗?
  • @Hiten 请将您的代码作为建议的答案发布,因为它更易于阅读(格式化代码),我相信您的答案是正确的 - 这就是我会发布的。
  • 我试过这段代码,但这段代码根本没有最大化浏览器。

标签: google-chrome selenium automation


【解决方案1】:

这里的主要问题似乎是 Dimension() 构造函数中这个 1200 和 600 使用的单位是什么?

类维度

根据文档Dimension Class 定义为:

public class Dimension
extends java.lang.Object
Similar to Point - implement locally to avoid depending on GWT.

类点

根据文档Point 定义为:

A copy of java.awt.Point, to remove dependency on awt.

java.awt.Point

根据文档java.awt.Point 定义为:

public class Point
extends Point2D
implements Serializable

A point representing a location in (x,y) coordinate space, specified in integer precision.

Point2D 类

根据文档Point2D Class 定义为:

The Point2D class defines a point representing a location in (x,y) coordinate space.
This class is only the abstract superclass for all objects that store a 2D coordinate. 
The actual storage representation of the coordinates is left to the subclass.

进一步挖掘,this documentation 说:

点是空间中特定位置的规范。它既没有高度,也没有宽度,也没有深度。因此,它不能在您的计算机屏幕上呈现,尽管可以在您的屏幕上呈现占据通常由该点指定的空间的像素。因此,我们 2D 空间中的一个点表示该空间中的一个位置,通常由一对坐标值指定,水平 (x) 和垂直 (y)。

这类似于在笛卡尔坐标中执行图形操作的概念,唯一的区别是,在笛卡尔坐标中,y 位移的正方向通常是向上的,而在我们当前的参考系中,正 y 位移的方向向下。与典型的笛卡尔坐标一样,正 x 位移的方向是向右。

【讨论】:

    【解决方案2】:
     System.setProperty("webdriver.chrome.driver", "chromedriver"); 
     ChromeOptions options = new ChromeOptions(); 
     options.addArguments("window-size=800,480"); 
     DesiredCapabilities cap = DesiredCapabilities.chrome(); 
     cap.setCapability(ChromeOptions.CAPABILITY, options);
    
     driver = new ChromeDriver(cap);
     driver.get("https://google.com");
    

    试试这个代码。 我试过了,效果很好。

    【讨论】:

    • 我试过这个,但它是在创建 webdriver 对象之前。但是如何设置已经存在的驱动程序对象(浏览器)的大小?
    • 您只需要设置现有驱动程序的功能。
    • 你能给一个示例代码吗?我无法为现有驱动程序设置。
    • 在我的例子中,它正在创建一个新的浏览器并离开现有的浏览器。
    • 驱动 = 新的 ChromeDriver(cap);您只需要在初始化时在现有驱动程序中传递“cap”即可。
    猜你喜欢
    • 2021-08-19
    • 2013-07-24
    • 2016-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-19
    相关资源
    最近更新 更多