必须验证页面上显示的图像

yk9xbfzb  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(402)

我有一个特定的url示例https://www.gnostice.com/nl_article.asp?id=207&t=how_to_read_a_pdf_file_from_a_url_in_java (url中的图像不在任何文件夹位置)
验证的图像规格要求:
5英寸宽=672
7英寸宽=840
我有27个可用的<img,必须检查:
5英寸宽=672的图像有多少
7英寸宽=840的图像有多少
有其他尺寸的图像吗
你能告诉我如何使用java selenium验证它吗。。。谢谢您

mxg2im7a

mxg2im7a1#

package selenium;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

public class Tifoso {

    // in my case %USERPROFILE%\eclipse-workspace\Selenium
    public static String userDir = System.getProperty("user.dir");

    // chromedriver.exe location
    public static String chromedriverPath = userDir + "\\resources\\chromedriver.exe";  

    public static WebDriver driver;

    // standard chromedriver initialization
    public static WebDriver startChromeDriver() {
        System.setProperty("webdriver.chrome.driver", chromedriverPath);
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--start-maximized");
        options.addArguments("--ignore-certificate-errors");        
        driver = new ChromeDriver(options);

        // setting 30 seconds wait to load pages
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

        driver.manage().window().maximize();
        return driver;
    }

    public static void main(String[] args) {

        WebDriver driver = startChromeDriver();
        String url = "https://www.gnostice.com/nl_article.asp?id=207&t=How_To_Read_A_PDF_File_From_A_URL_In_Java";
        driver.get(url);

        // empty lists
        List<WebElement> fiveInchWidthImages = new ArrayList<WebElement>();
        List<WebElement> sevenInchWidthImages = new ArrayList<WebElement>();
        List<WebElement> otherInchWidthImages = new ArrayList<WebElement>();

        // getting all images
        List<WebElement> allImages = driver.findElements(By.tagName("img"));
        for (WebElement image: allImages) {
            // getting image width
            int width = image.getRect().getWidth();
            if (width == 672) {
                fiveInchWidthImages.add(image);
            }
            else if (width == 820) {
                sevenInchWidthImages.add(image);
            }
            else {
                otherInchWidthImages.add(image);
            }
        }

        System.out.println("Number of images with width 672 is " + fiveInchWidthImages.size());
        System.out.println("Number of images with width 820 is " + sevenInchWidthImages.size());
        System.out.println("Number of images with other width is " + otherInchWidthImages.size());

        driver.close();
    }

}

输出:

Starting ChromeDriver 87.0.4280.88 (89e2380a3e36c3464b5dd1302349b1382549290d-refs/branch-heads/4280@{#1761}) on port 4826
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
Pro 17, 2020 8:25:10 DOP. org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
Number of images with width 672 is 0
Number of images with width 820 is 0
Number of images with other width is 27

相关问题