package selenium;
import java.awt.Dimension;
import java.awt.Toolkit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
public class Penzov extends WebDriverSetup {
public static void main(String[] args) {
WebDriver driver = startChromeDriver();
driver.get("https://stackoverflow.com/");
WebElement logo = driver.findElement(By.xpath("//a[@href='https://stackoverflow.com']"));
int logoX = logo.getLocation().getX();
int logoY = logo.getLocation().getY();
int logoWidth = logo.getRect().getWidth();
int logoHeight = logo.getRect().getHeight();
Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
int screenWidth = (int) dimension.getWidth();
int screenHeight = (int) dimension.getHeight();
System.out.println("Logo is " + logoX + " pixels from left border.");
System.out.println("Logo is " + (screenWidth - logoX + logoWidth) + " pixels from right border.");
System.out.println("Logo is " + logoY + " pixels from top border.");
System.out.println("Logo is " + (screenHeight - logoY + logoHeight) + " pixels from bottom border.");
driver.quit();
}
}
输出:
Starting ChromeDriver 86.0.4240.22 (398b0743353ff36fb1b82468f63a3a93b4e2e89e-refs/branch-heads/4240@{#378}) on port 12429
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
Pro 03, 2020 9:57:25 DOP. org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
Logo is 363 pixels from left border.
Logo is 1723 pixels from right border.
Logo is 3 pixels from top border.
Logo is 1124 pixels from bottom border.
1条答案
按热度按时间gzszwxb41#
是的,使用
element.getLocation()
.输出: