java 如何在java中使用selenium webdriver查找图像标签以及如何检查它是否已正确显示?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16170720/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to find an image tag using selenium webdriver in java and how to check if it has been properly displayed?
提问by Dcoder
I need to check a webpage for all the images and get their attributes like src and width etc using selenium webdriver. Can someone please help me with it ?
我需要检查所有图像的网页并使用 selenium webdriver 获取它们的属性,如 src 和 width 等。有人可以帮我吗?
回答by Pavel Janicek
I will give you few ideas:
我会给你一些想法:
1st I would start with findElements
method which returns all elements:
第一,我将从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...
然后你有这些值存储:) 然后你可以使用与上面提到的相同的方式迭代它们......
NOTEI 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
回答by Jamie Piltz
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". However I often create objects that abstract the WebElement object (this may or may not be a good thing).
正如 Pavel Janicek 所说,您必须找到与标签“img”匹配的所有 WebElement。然而,我经常创建抽象 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:
然后您将遍历 WebElements 作为 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;
}