从浏览器边框验证按钮填充

f1tvaqid  于 2021-07-07  发布在  Java
关注(0)|答案(1)|浏览(274)

我有一个小的网络登录按钮和标志形式。
有没有可能得到按钮和标志的填充空间。我想验证一下,按钮是不是接近网页浏览器帧超过10个像素。这可能吗?

gzszwxb4

gzszwxb41#

是的,使用 element.getLocation() .

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.

相关问题