Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何选中所有复选框?试图勾选所有复选框,测试通过,但从视觉上看,我看不到它们已被勾选 @测试 public void checkAllFilters()引发InterruptedException{ couponSearchPage.navigateToAvailableCoupons(); couponSearchPage.refineBarClick(); driver.findElement(By.name(“group1”)).isDisplayed(); WebElement checkBoxList=driver.findElement(By.name(“group1”); System.out.println(“是否显示复选框?”+复选框列表.isDisplayed()); List checkBoxFiltersList=driver.findElements(By.xpath(“//div[@class='c-checkbox']//following::input[@type='checkbox']); 用于(WebElement复选框:checkBoxFiltersList){ 《睡眠》(2000年); 复选框。单击(); 《睡眠》(2000年); //主页面.ajax单击(复选框); } //如果选中任何复选框,则断言 List allCheckboxList=driver.findElements(By.xpath(“//div[@class='c-checkbox']//following::input[@type='checkbox']); 如果(!allCheckboxList.isEmpty()){ Assert.fail(); } }_Java_Selenium_Selenium Webdriver - Fatal编程技术网

Java 如何选中所有复选框?试图勾选所有复选框,测试通过,但从视觉上看,我看不到它们已被勾选 @测试 public void checkAllFilters()引发InterruptedException{ couponSearchPage.navigateToAvailableCoupons(); couponSearchPage.refineBarClick(); driver.findElement(By.name(“group1”)).isDisplayed(); WebElement checkBoxList=driver.findElement(By.name(“group1”); System.out.println(“是否显示复选框?”+复选框列表.isDisplayed()); List checkBoxFiltersList=driver.findElements(By.xpath(“//div[@class='c-checkbox']//following::input[@type='checkbox']); 用于(WebElement复选框:checkBoxFiltersList){ 《睡眠》(2000年); 复选框。单击(); 《睡眠》(2000年); //主页面.ajax单击(复选框); } //如果选中任何复选框,则断言 List allCheckboxList=driver.findElements(By.xpath(“//div[@class='c-checkbox']//following::input[@type='checkbox']); 如果(!allCheckboxList.isEmpty()){ Assert.fail(); } }

Java 如何选中所有复选框?试图勾选所有复选框,测试通过,但从视觉上看,我看不到它们已被勾选 @测试 public void checkAllFilters()引发InterruptedException{ couponSearchPage.navigateToAvailableCoupons(); couponSearchPage.refineBarClick(); driver.findElement(By.name(“group1”)).isDisplayed(); WebElement checkBoxList=driver.findElement(By.name(“group1”); System.out.println(“是否显示复选框?”+复选框列表.isDisplayed()); List checkBoxFiltersList=driver.findElements(By.xpath(“//div[@class='c-checkbox']//following::input[@type='checkbox']); 用于(WebElement复选框:checkBoxFiltersList){ 《睡眠》(2000年); 复选框。单击(); 《睡眠》(2000年); //主页面.ajax单击(复选框); } //如果选中任何复选框,则断言 List allCheckboxList=driver.findElements(By.xpath(“//div[@class='c-checkbox']//following::input[@type='checkbox']); 如果(!allCheckboxList.isEmpty()){ Assert.fail(); } },java,selenium,selenium-webdriver,Java,Selenium,Selenium Webdriver,尝试勾选页面上的所有复选框,但它不勾选,即使测试通过。Xpth不正确或我需要ajaxClcick? @Test public void checkAllFilters() throws InterruptedException { couponSearchPage.navigateToAvailableCoupons(); couponSearchPage.refineBarClick(); driver.findElement(By.name("group1")).is

尝试勾选页面上的所有复选框,但它不勾选,即使测试通过。Xpth不正确或我需要ajaxClcick?

@Test
public void checkAllFilters() throws InterruptedException {
    couponSearchPage.navigateToAvailableCoupons();
    couponSearchPage.refineBarClick();
    driver.findElement(By.name("group1")).isDisplayed();
    WebElement checkBoxList = driver.findElement(By.name("group1"));
    System.out.println("Is checkbox displayed? " + checkBoxList.isDisplayed());

    List<WebElement> checkBoxFiltersList = driver.findElements(By.xpath("//div[@class='c -checkbox']//following:: input[@type='checkbox']"));
    for(WebElement checkBox : checkBoxFiltersList){
        Thread.sleep(2000);
        checkBox.click();
        Thread.sleep(2000);
        //mainPage.ajaxClick(checkBox);
    }
    // Assert if any checkbox left checked
    List<WebElement> allCheckboxList = driver.findElements(By.xpath("//div[@class='c-checkbox']//following:: input[@type='checkbox']"));
    if(!allCheckboxList.isEmpty()){
        Assert.fail();
    }

}

婴儿及;育儿

单击
标签
元素不在
输入上

<div class="c-checkbox">
     <input type="checkbox" id="Baby &amp; ChildCare" class="c-settings-form__filter-checkbox c-checkbox__input js-update-filter" name="filter-Baby &amp; ChildCare" value="Baby &amp; ChildCare">
     <label for="Baby &amp; ChildCare" class="c-checkbox__label"><span></span>Baby &amp; ChildCare</label>
</div>

更新,使用不同断言编写代码,例如:

Assert.assertEquals(driver.findElements(By.cssSelector(".c-checkbox input[checked='true']")).size(), 
            checkBoxFiltersList.size());
WebDriverWait wait=newwebdriverwait(驱动程序,10);
driver.findElement(By.cssSelector(“.c-sort-filter”))。单击();
列表复选框=wait.until(通过.cssSelector(“.c-checkbox标签”));
列表标签=复选框.stream().map(WebElement::getText).collect(Collectors.toList());
用于(字符串标签:标签){
driver.findelelement(By.cssSelector(String.format(“label[for='%s']”,label)))。单击();
//等待加载消失
等待.until(预期条件.InvisibilityFelementLocated(由.cssSelector(“.c-spinner”));
//检查是否选择了输入
wait.until(ExpectedConditions.elementToBeSelected(由.cssSelector(String.format(“输入[id='%s'],标签)));
}
//检查是否通过使用复选框比较标记选择了输入
List selectedFilters=driver.findelelements(By.cssSelector(“.divider view filters List.c-xbubble”)).stream().map(WebElement::getText).collect(collector.toList());
Assert.assertEquals(选择的过滤器、标签);

单击
标签
元素不在
输入上

<div class="c-checkbox">
     <input type="checkbox" id="Baby &amp; ChildCare" class="c-settings-form__filter-checkbox c-checkbox__input js-update-filter" name="filter-Baby &amp; ChildCare" value="Baby &amp; ChildCare">
     <label for="Baby &amp; ChildCare" class="c-checkbox__label"><span></span>Baby &amp; ChildCare</label>
</div>

更新,使用不同断言编写代码,例如:

Assert.assertEquals(driver.findElements(By.cssSelector(".c-checkbox input[checked='true']")).size(), 
            checkBoxFiltersList.size());
WebDriverWait wait=newwebdriverwait(驱动程序,10);
driver.findElement(By.cssSelector(“.c-sort-filter”))。单击();
列表复选框=wait.until(通过.cssSelector(“.c-checkbox标签”));
列表标签=复选框.stream().map(WebElement::getText).collect(Collectors.toList());
用于(字符串标签:标签){
driver.findelelement(By.cssSelector(String.format(“label[for='%s']”,label)))。单击();
//等待加载消失
等待.until(预期条件.InvisibilityFelementLocated(由.cssSelector(“.c-spinner”));
//检查是否选择了输入
wait.until(ExpectedConditions.elementToBeSelected(由.cssSelector(String.format(“输入[id='%s'],标签)));
}
//检查是否通过使用复选框比较标记选择了输入
List selectedFilters=driver.findelelements(By.cssSelector(“.divider view filters List.c-xbubble”)).stream().map(WebElement::getText).collect(collector.toList());
Assert.assertEquals(选择的过滤器、标签);


您可以发布一个链接到您正在自动运行的页面吗?很遗憾,我不能。我只需要弄清楚如何使用上面的HTML代码创建正确的xPath。上面HTML代码的“正确xPath”是
//div[@class='c-checkbox']/input
。但我假设这不起作用,这就是为什么您在这里——因此,需要查看完整页面的HTML或链接。如果没有整个页面外观的更大上下文,就不可能知道您当前选择的XPath为什么不能正常工作。基本上,我必须单击每个“优化->筛选”选项。您可以发布一个链接到您正在自动化的页面吗?不幸的是,我不能。我只需要弄清楚如何使用上面的HTML代码创建正确的xPath。上面HTML代码的“正确xPath”是
//div[@class='c-checkbox']/input
。但我假设这不起作用,这就是为什么您在这里——因此,需要查看完整页面的HTML或链接。如果没有整个页面外观的更大上下文,就不可能知道您当前选择的XPath为什么不能正常工作。基本上,我必须单击每个优化->筛选选项。如果我使用此XPath,它会单击第一个,失败后。
org.openqa.selenium.StaleElementReferenceException:stale元素引用:元素未附加到页面文档
。我使用了ajax单击方法,同样-它单击第一个选项后失败。因为您必须等待JavaScripts完成。为此,您可以使用
WebDriverWait
。谢谢你的回答。我更改了xPath并添加了断言测试通过,但我没有看到复选框被选中。请检查答案更新。您可以使用您想要的断言,或者在不同的测试用例中拆分它。如果我使用此xPath,它会单击第一个断言,然后在它失败后。
org.openqa.selenium.StaleElementReferenceException:stale元素引用:元素未附加到页面文档
。我使用了ajax单击方法,同样-它单击第一个选项后失败。因为您必须等待JavaScripts完成。为此,您可以使用
WebDriverWait
。谢谢你的回答。我更改了xPath并添加了断言测试通过,但我没有看到复选框被选中。请检查答案更新。您可以使用您想要的断言,或者在不同的测试用例中拆分它