【发布时间】:2019-02-04 09:08:16
【问题描述】:
我想获取 WebElement 的屏幕坐标并使用机器人类点击它。
SeleniumMethods sl= new SeleniumMethods();
WebDriver driver = new FirefoxDriver();
public void example () throws Exception{
driver.get("http://www.example.com/");
driver.manage().window().maximize();
//Xpath to more Info Link
String xpath = "/html/body/div/p[2]/a";
Robot robot = new Robot();
//Pass in the X and Y Coordinates of the Element (Integer)
robot.mouseMove(driver.findElement(By.xpath(xpath)).getLocation().getX(),driver.findElement(By.xpath(xpath)).getLocation().getY());
robot.mousePress(InputEvent.BUTTON1_MASK);
Thread.sleep(50);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
}
它认为问题在于 mousePress 方法中传递的坐标不包含 firefox 选项卡、url 栏等。这真的是我的问题吗?如果是这样,我该如何解决? 提前致谢!
【问题讨论】:
-
找到具有 .size 和 .location 属性的元素后。
-
我使用 .getLocation .getSize 如何帮助我?
-
我不明白你为什么除了 WebDriver 之外还使用机器人,但你为什么不开始分割线 robot.mouseMove(driver.findElement(By.xpath(xpath)).getLocation ().getX(),driver.findElement(By.xpath(xpath)).getLocation().getY());看看你能找到元素,然后返回位置,这样你就可以找到确切的问题。
-
我确实使用了 .getLocation,它返回的值似乎不是屏幕尺寸,而是没有标签和栏的浏览器
标签: java selenium-webdriver point awtrobot