URL - https://www.etmoney.com/mutual-funds/filter/long-term-funds-return
任务1:获取所有返回,并验证是否按降序排序。
List<WebElement> returns = driver.findElements(By.xpath("//tr[@class='mfFund-nav-row even']//td[11]"));
for(int ii=0; i<returns.size(); i++) {
String returnDesc = returns.get(i).getText();
System.out.println(returnDesc);
}以上代码只交替刮取数据,即有12个元素,但只选择7-8个元素。
任务2:在返回部分中验证是否选择了“长期历史返回”。
WebElement historicReturn = driver.findElement(By.xpath("//label[normalize-space()='Long-Term Historic Returns']"));
Assert.assertTrue(historicReturn.isSelected());无法验证上述代码,因为它每次都返回false,即使元素被选中。
发布于 2022-09-05 13:23:34
对于任务1,您可以使用下面的代码使用java8首先从表中获取"Scheme name“列表,将可见值收集到一个列表中。现在为元素创建两个带有文本的列表,并将其中一个按降序排序,现在使用等于来比较列表,如果顺序是降序,那么它应该将true打印到控制台
//get the Table elements
List<WebElement> returns = driver.findElements(
By.xpath("//tr[@class='mfFund-nav-row']//td[1]//a")).stream()
.filter(WebElement::isDisplayed).collect(Collectors.toList());
//Store text in a Descending order in list
List<String> decsSorted = returns.stream().map(WebElement::getText)
.sorted(Comparator.reverseOrder()).collect(Collectors.toList());
//Store text in a actual order in list
List<String> actualList = returns.stream().map(WebElement::getText)
.collect(Collectors.toList());
//compare and print
System.out.println(actualList.equals(decsSorted));对于任务2,检查元素的方式与下面的find子div元素类似,如果子div元素类更改为"radiobox active“,则意味着它已被选中,如果选中,则将得到元素,否则它将抛出到NoSuchElementException以下。
try {
driver.findElement(
By.xpath("//label[normalize-space()='Long-Term Historic Returns']"))
.findElement(By.xpath(".//div[@class= \"radiobox active\"]"));
System.out.println(true);
} catch (NoSuchElementException e) {
System.out.println(false);
}https://stackoverflow.com/questions/73511496
复制相似问题