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
1条答案
按热度按时间mxg2im7a1#
输出: