java返回html路径页面位置,通过使用Java Selenium WebDriver和HTML窗口位置

小编典典

我相信无法获得页面上元素的真实屏幕位置。

我也认为全屏模式是您最好的选择。

就是说,我写了一个RobotCalibration可以检测当前浏览器实际偏移量的类。它会打开一个特制页面,并使用Robot该类单击它。该算法从浏览器的中心开始,然后使用二等分线找到浏览器视口的左上角。

在IE8和FF18上测试。适用于最大化和窗口浏览器。已知问题:如果启用了顶部的书签工具栏,则它可能会单击某些书签,因此会重定向。它可以很容易地处理,但是如果您需要它,我留给您:)。

测试页面:

Calibration Test

οnclick="document.getElementById('done').value = 'yep'" />

该RobotCalibration班。它有点长,所以我建议您将其复制粘贴到您喜欢的IDE中并在其中进行探索:

import java.awt.AWTException;

import java.awt.Dimension;

import java.awt.Point;

import java.awt.Robot;

import java.awt.Toolkit;

import java.awt.event.InputEvent;

import java.nio.file.Paths;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.ie.InternetExplorerDriver;

public class RobotCalibration {

public static Point calibrate(WebDriver driver) {

return new RobotCalibration(driver).calibrate();

}

/** Time for which to wait for the page response. */

private static final long TIMEOUT = 1000;

private final WebDriver driver;

private final Robot r;

private final Point browserCenter;

private int leftX;

private int rightX;

private int midX;

private int topY;

private int bottomY;

private int midY;

private RobotCalibration(WebDriver driver) {

this.driver = driver;

try {

driver.manage().window().getSize();

} catch (UnsupportedOperationException headlessBrowserException) {

throw new IllegalArgumentException("Calibrating a headless browser makes no sense.", headlessBrowserException);

}

try {

this.r = new Robot();

} catch (AWTException headlessEnvironmentException) {

throw new IllegalStateException("Robot won't work on headless environments.", headlessEnvironmentException);

}

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

org.openqa.selenium.Dimension browserSize = driver.manage().window().getSize();

org.openqa.selenium.Point browserPos = driver.manage().window().getPosition();

// a maximized browser returns negative position

// a maximized browser returns size larger than actual screen size

// you can't click outside the screen

leftX = Math.max(0, browserPos.x);

rightX = Math.min(leftX + browserSize.width, screenSize.width - 1);

midX = (leftX + rightX) /2;

topY = Math.max(0, browserPos.y);

bottomY = Math.min(topY + browserSize.height, screenSize.height - 1);

midY = (topY + bottomY) /2;

browserCenter = new Point(midX, midY);

}

private Point calibrate() {

driver.get(Paths.get("files/RobotCalibration.html").toUri().toString());

// find left border

while (leftX < rightX) {

click(midX, midY);

if (clickWasSuccessful()) {

rightX = midX;

} else {

leftX = midX + 1;

// close any menu we could have opened

click(browserCenter.x, browserCenter.y);

}

midX = (leftX + rightX) /2;

}

// find top border

while (topY < bottomY) {

click(midX, midY);

if (clickWasSuccessful()) {

bottomY = midY;

} else {

topY = midY + 1;

// close any menu we could have opened

click(browserCenter.x, browserCenter.y);

}

midY = (topY + bottomY) /2;

}

if (!isCalibrated()) {

throw new IllegalStateException("Couldn't calibrate the Robot.");

}

return new Point(midX, midY);

}

/** clicks on the specified location */

private void click(int x, int y) {

r.mouseMove(x, y);

r.mousePress(InputEvent.BUTTON1_DOWN_MASK);

r.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);

// for some reason, my IE8 can't properly register clicks that are close

// to each other faster than click every half a second

if (driver instanceof InternetExplorerDriver) {

sleep(500);

}

}

private static void sleep(int millis) {

try {

Thread.sleep(millis);

} catch (InterruptedException ignored) {

// nothing to do

}

}

private int counter = 0;

/** @return whether the click on a page was successful */

private boolean clickWasSuccessful() {

counter++;

long targetTime = System.currentTimeMillis() + TIMEOUT;

while (System.currentTimeMillis() < targetTime) {

int pageCounter = Integer.parseInt(driver.findElement(By.id("counter")).getAttribute("value"));

if (counter == pageCounter) {

return true;

}

}

return false;

}

/** @return whether the top left corner has already been clicked at */

private boolean isCalibrated() {

long targetTime = System.currentTimeMillis() + TIMEOUT;

while (System.currentTimeMillis() < targetTime) {

if (driver.findElement(By.id("done")).getAttribute("value").equals("yep")) {

return true;

}

}

return false;

}

}

用法示例:

WebDriver driver = new InternetExplorerDriver();

Point p = RobotCalibration.calibrate(driver);

System.out.println("Left offset: " + p.x + ", top offset: " + p.y);

driver.quit();

如果有不清楚的地方,请随时提出任何问题。

2020-06-26

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值