【发布时间】:2014-06-24 00:53:50
【问题描述】:
我正在尝试获取节类中存在的所有图像的属性。我要获取的图像属性是:
图像高度。
图像宽度
图片来源
我正在尝试使用 Selenium Webdriver 来实现。
我是 Selenium 的新手,所以我浏览了网络教程和互联网上提供的各种答案。
我的代码是这样的:
public void findAllImages(){
driver=new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
baseUrl="http://northeastindiaholidays.com/";
driver.get(baseUrl);
WebElement menu=driver.findElement(By.className("destinations clearfix full"));
List<WebElement> allImages=driver.findElements(By.tagName("img"));
List<String> widthofImage = new ArrayList<String>();
List<String> heightofImage = new ArrayList<String>();
List<String> srcofImage = new ArrayList<String>();
for (WebElement imageFromList: allImages){
widthofImage.add(imageFromList.getAttribute("width"));
heightofImage.add(imageFromList.getAttribute("height"));
srcofImage.add(imageFromList.getAttribute("src"));
System.out.println(widthofImage);
System.out.println(heightofImage);
System.out.println(srcofImage);
}
}
}
页面 html 如下所示:
<section class="destinations clearfix full" style="margin-bottom:10px!important">
<h1>Beach Destinations</h1>
<article class="location_item one-fourth fluid-item">
<figure>
<a title="" href="http://northeastindiaholidays.com/?location=goa">
<img width="270" height="152" alt="" src="http://northeastindiaholidays.com/wp- content/uploads/2014/06/Goa.gif"/>
</a>
</figure>
<div class="details">
</article>
页面的网址是:North East India Holidays
【问题讨论】:
-
你有什么问题吗?
-
是的,我意识到我完全错过了描述问题。我在使用目标部分类选择类名时出错。这是我收到的错误图片的链接 [link]drive.google.com/file/d/0B3aMk3yP4IBFNW8tV0FqRXRPWnc/…
-
请用新信息编辑原始帖子。另外:您真的截取了文本窗口的屏幕截图吗?为什么?
-
我认为错误说明了一切:“不支持复合类名。”大概在这一行:
driver.findElement(By.className("destinations clearfix full"));。错误输出的其余部分会告诉您解决方案。 -
使用 CSS 选择器“.destination.clearfix.full”。这将找到具有所有 3 个类的任何元素。即使类的顺序发生变化,它也可以工作。 Xpath 不那么宽容
标签: user-interface selenium selenium-webdriver