Selenium Java-检查Weblist元素的存在/不存在

时间:2017-04-04 08:18:03

标签: java selenium selenium-webdriver automation ui-automation

我需要自动化场景,我需要点击展开全部按钮&检查所有过滤器是否已展开,然后单击全部折叠按钮并检查是否所有过滤器都已折叠。

我使用的方法是将所有过滤器放在网表中,获取其大小和数量。然后使用assert方法检查它是否非空,但是为了检查崩溃功能,这种方法失败了。

有没有办法检查webelements列表的可见性或存在,就像我们对单个webelement有isDisplayed()一样?

以下是我尝试的上述逻辑的代码:

driver.findElement(By.linkText("Expand All")).click();
Thread.sleep(3000);
System.out.println("Clicking on the Expand All option");

Assert.assertFalse(driver.findElements(By.cssSelector("input[type='checkbox']")).isEmpty(),"The Filters are not expanded"); 

int filters_max_size=driver.findElements(By.cssSelector("input[type='checkbox']")).size();

System.out.println("There are total "+filters_max_size+ "filters expanded");

System.out.println("Clicking on the Collapse All option");
Reporter.log("Clicking on the Collapse All option");

driver.findElement(By.id("collapse_all")).click();
Thread.sleep(3000);

Assert.assertTrue(driver.findElements(By.cssSelector("input[type='checkbox']")).isEmpty(),"The Filters are not collapsed"); 
Assert.assertEquals(0, driver.findElements(By.cssSelector("input[type='checkbox']")).,"The Filters are not collapsed");


<a class="refinement_anchor" href="/search?button=search&expand_all=5B%5D=%7B%22s%22%3A%5B%7B%22id%22%3A%22%22%2C%22xPath%22%3A%22%24source_type%22%2C%22logic%22%3A%22OR%22%2C&utf8=%E2%9C%93">
<input id="refinement_214391870" class="flex" type="checkbox" name="refinement_214391870">
<label class="text" for="refinement_214391870">
<div class="text refinement_information">
<span class="label" data-token="{"s":[{"id":"refine_source_type","xPath":"$source_type","logic":"OR","s":[{"n":"Selective"}]}]}" title="Selective"> Selective </span>
<span class="ndocs"> (136,292) </span>
</div>
</label>
</a>
</li>
<li class="text no_children ">
</ul>
<a class="show_all" href="/search/show_all?button=search&id=&q=&types=&utf8=%E2%9C%93&widget_id=refine_source_type">Show all</a>
</div>

2 个答案:

答案 0 :(得分:0)

您可以尝试如下:

 driver.findElement(By.id("collapse_all")).click();
 Thread.sleep(3000);

WebElement allFilters = driver.findElements(By.cssSelector("input[type='checkbox']"));
for(WebElement filter : allFilters) {

 if(filter.isDisplayed()){

   System.out.println(filter.getText() + " is still displayed." );
   break;
 }}

答案 1 :(得分:0)

您可以在单击全部折叠后使用以下代码为每个复选框验证是否显示复选框。如果元素显示,则下面的语句将返回true,否则它将返回false(在您的场景中折叠时)< / p>

elem.isDisplayed()