java - Selenium Java - 检查 Weblist 元素是否存在

标签 java selenium selenium-webdriver automation ui-automation

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

我使用的方法是将所有过滤器放入网络列表中,获取其大小,然后使用断言方法检查其是否为非空,但对于检查折叠功能,相同的方法失败了。

是否有一种方法可以检查 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>

最佳答案

您可以尝试如下:

 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;
 }}

关于java - Selenium Java - 检查 Weblist 元素是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43201824/

相关文章:

python - 对 Selenium 使用随机 userAgent(python)

java - org.openqa.selenium.firefox.FirefoxDriver 类型不可访问

java - 无法通过 Selenium 和 Java 在 https ://spicejet. com 中选择出发日期

iframe - 如何使用 Behat/Mink 切换到动态命名的 iframe

java - 如何管理/停止 spring 4 ConcurrentTaskScheduler

java - 在netbeans框架中制作JList

java - 在 Kotlin 中将映射转换为对象

java - 在 Selenium 中使用 Java 从 html 标签接收数据

java - swt 表对单元格中的文本长度有限制吗?

javascript - 焦点问题使测试失败