如何使用Selenium和Java查找并单击“选择布尔值”复选框

时间:2019-03-15 06:27:51

标签: java selenium selenium-webdriver xpath css-selectors

我试图单击使用id的质数选择boolean复选框,但我无法单击,但由于以下原因而导致出现异常:

org.openqa.selenium.NoSuchElementException: Cannot locate an element using By.id: runDARtest1

我也增加了第二30名,但又遇到了同样的问题

driver.get("http://localhost:5080/MOIA2-WEB/");
driver.findElement(By.id("moia2Form:loginForm:tabViedID:userName")).sendKeys("cherie.chen");
driver.findElement(By.id("moia2Form:loginForm:tabViedID:password")).sendKeys("Password1");
driver.findElement(By.id("moia2Form:loginForm:tabViedID:loginButton")).click();
driver.findElement(By.id("moia2Form:lnkMarketClearing")).click();
driver.findElement(By.id("moia2Form:system_Result_runinfo")).click();
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.id("runDARtest1"))).click();;

Xhtml代码:选中表格内部的布尔复选框

<tr>
    <td><label class="switch"> <h:selectBooleanCheckbox
                                            class="runDARclass"
                                            value="#{systemResultManagedBean.runDAR}" id="runDAR" />
    <span class="slider round" id="runDARtest1"></span>
    </label></td>
    <td><p:spacer width="5px" /><span>DAR</span></td>
</tr>

1 个答案:

答案 0 :(得分:0)

所需元素是动态元素,因此在元素上click()上必须诱使 WebDriverWait 使元素可点击,并且您可以使用任一元素以下解决方案:

  • cssSelector

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("label.switch span.slider.round#runDARtest1"))).click();
    
  • xpath

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//label[@class='switch']//span[@class='slider round' and @id='runDARtest1']"))).click();