简体   繁体   English

如何在Java中使用Selenium WebDriver查找图像标签,以及如何检查它是否已正确显示?

[英]how to find an image tag using selenium webdriver in java and how to check if it has been properly displayed?

I need to check a webpage for all the images and get their attributes like src and width etc using selenium webdriver. 我需要检查网页上的所有图像,并使用硒webdriver获取其属性,如src和width等。 Can someone please help me with it ? 有人可以帮我吗?

I will give you few ideas: 我会给你一些想法:

1st I would start with findElements method which returns all elements: 首先,我将从findElements方法开始, findElements方法返回所有元素:

 List<WebElement> allImages = driver.findElements(By.ByTagName("img"));
 List<String> widthofImage = new ArrayList<String>;
 List<String> heighthOfImage = new ArrayList<String>;

Then you will have to iteratwe through the list 然后,您将不得不遍历列表

 for (WebElement imageFromList: allImages){
   widthOfImage.add(imageFromList.getAttribute("width"));
   heighthOfImage.add(imageFromList.getAttribute("height"));
   //.. etc

 }

And then you have these values stored :) You can then iterate throuh them using same way as noted above... 然后就可以存储这些值了:)然后可以使用上述相同的方法遍历它们。

NOTE I am writing this from top of the head. 注意我是从头开始写的。 So please double check the code 所以请仔细检查代码

BTW the driver variable is assumed healthy and living instance of WebDriver 顺便说一句,假设driver变量是WebDriver健康存在实例

Finding elements by Tag and getting their Attributes 通过标签查找元素并获取其属性

As Pavel Janicek says you will have to find all of the WebElements matching the tag "img". 正如Pavel Janicek所说,您将必须找到所有与标签“ img”匹配的WebElement。 However I often create objects that abstract the WebElement object (this may or may not be a good thing). 但是,我经常创建抽象WebElement对象的对象(这可能是好事,也可能不是好事)。

For example, the following objects may abstract an Image as a type of WebElement (you may extend WebElement if you wish): 例如,以下对象可以将Image抽象为WebElement类型(如果需要,可以扩展WebElement):

public class BasicElement {
    protected WebElement element;

    public BasicElement(WebElement element) {
        this.element = element;
    }
}

public class Image extends BasicElement {

    private String width;
    private String height;
    private String src;

    public Image(WebElement element) {
        super(element);

        assignWidth();
        assignHeight();
        assignHeight();
    }


    private void assignWidth() {
        width = element.getAttribute("width");
    }

    private void assignHeight() {
        height = element.getAttribute("height");
    }

    private void assignSrc() {
        src = element.getAttribute("src");
    }

    // various getters, setters and image related functionality

}

And then you would iterate through the WebElements as Image objects and assign their attributes like so: 然后您将遍历WebElement作为Image对象并分配它们的属性,如下所示:

List<Image> images;
List<WebElement> imageElements = driver.findElements(By.ByTagName("img"));
for (WebElement imageElement : imageElements) {
    images.add(new Image(imageElement));
}

This could probably be shortened if you extend WebElement. 如果扩展WebElement,则可能会缩短此时间。

Determining Visibility of Elements 确定元素的可见性

If you want to know if an element is visible then I suggest you implement a wait: 如果您想知道某个元素是否可见,那么建议您执行一个等待:

public WebElement waitForElementVisible(WebDriver driver, final By selector, int timeOutInSeconds) {
    WebElement element;
    try{
        WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
        element = wait.until(ExpectedConditions.visibilityOfElementLocated(selector));

        return element;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

Or if you want to know if the element exists: 或者,如果您想知道元素是否存在:

public static WebElement waitForElementPresent(WebDriver driver, final By selector, int timeOutInSeconds) {
    WebElement element;
    try{
        WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
        element = wait.until(ExpectedConditions.presenceOfElementLocated(selector));

        return element;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Java 检查下拉列表中的选项在 Selenium Webdriver 中显示两次 - How to check that the options in dropdown are displayed twice in Selenium Webdriver using Java 如何使用 selenium webdriver 检查文件是否已成功下载? - How to check if a file has been downloaded successfully with selenium webdriver? 如何使用Selenium Java WebDriver选中复选框? - How to check the checkboxes using the Selenium Java WebDriver? Selenium Webdriver(Java)-如何检查findelement中是否存在标签元素 - Selenium Webdriver (Java) - how to check if tag element exists in findelement 如何<a class </a>在Selenium WebDriver中使用Java</a>单击带有标签名称的链接 - How to click a link with Tag name <a class </a> using Java in Selenium WebDriver 如何使用 Java 在 Selenium WebDriver 中计算 HTML 子标签 - How to count HTML child tag in Selenium WebDriver using Java 如何在java上使用selenium webdriver动态创建li标签 - How to create a li tag dynamically using selenium webdriver on java 如何使用Java使用Selenium Webdriver在此html中获取标记值? - How to get the <b> tag value in this html with Selenium Webdriver using Java? 如何使用 Java 在 Selenium WebDriver 中检查元素是否可点击 - How to check if element is clickable in Selenium WebDriver using Java 如何使用Java在Selenium WebDriver中检查必填字段是否为空? - How to check mandatory field whether it is empty or not in Selenium WebDriver using Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM