我想通过 selenium 网络驱动程序在我的网络应用程序中 select radio type checkbox 只要它可用
I would like to select radio type check-box in my web application through selenium web-driver whenever it is available
我想 select 通过 selenium web-driver 在我的 web 应用程序中选择无线电类型复选框
HTML:
<div class="enhanced-checkbox">
<input id="idYes" class="checkbox" type="radio" data-form-message="This is required" required="" value="Y" name="name">
<span></span>
</div>
它适用于以下网络驱动程序代码:
if (idYes.isElementPresentAndDisplayed())
((JavascriptExecutor)driver).executeScript("arguments[0].checked = true", idYes.isElementPresentAndDisplayed());
idYes.click();
我也尝试过:(在这种情况下单选按钮 selected 但在其他情况下不会忽略)
if (idYes.isElementPresentAndDisplayed()){
((JavascriptExecutor)driver).executeScript("arguments[0].checked = true", idYes.isElementPresentAndDisplayed());
idYes.click();}
或另一种尝试:(在本例中单选按钮未 selected)
if (idYes.isElementPresentAndDisplayed()){
((JavascriptExecutor)driver).executeScript("arguments[0].checked = true", idYes.isElementPresentAndDisplayed());
idYes.clickIfElement Present();}
但是当上述元素在不同场景下不可用时,则失败。
预期行为是:如果元素存在select或忽略。
将您的代码放在 try catch 块中。在 catch 块中,忽略发生的任何错误。
例如
try{
WebElement idYes = driver.findElement(By.id("idYes"));
if (idYes.isElementPresentAndDisplayed())
((JavascriptExecutor)driver).executeScript("arguments[0].checked = true",idYes.isElementPresentAndDisplayed());
idYes.click();
}catch(org.openqa.selenium.NoSuchElementException ignore){
//TODO: do nothing
}
希望对您有所帮助。
您也可以这样做:
WebDriverWait wait = new WebDriverWait(driver, 10); // Have a wait object with 10 sec(or whatever u prefer) to enforce on driver object stating for max time it should wait for the conditions
try{
wait.until(ExpectedConditions.elementToBeClickable(By.id("idYes"))).click(); // will explicitly wait for the element to be clickable, else throw timeout exception
}catch(TimeoutException toe){
System.out.println("Check box not present, hence skipping. " + toe); // here you can log or sysout the reason for skipping accompanied with exception
}
我想 select 通过 selenium web-driver 在我的 web 应用程序中选择无线电类型复选框 HTML:
<div class="enhanced-checkbox">
<input id="idYes" class="checkbox" type="radio" data-form-message="This is required" required="" value="Y" name="name">
<span></span>
</div>
它适用于以下网络驱动程序代码:
if (idYes.isElementPresentAndDisplayed())
((JavascriptExecutor)driver).executeScript("arguments[0].checked = true", idYes.isElementPresentAndDisplayed());
idYes.click();
我也尝试过:(在这种情况下单选按钮 selected 但在其他情况下不会忽略)
if (idYes.isElementPresentAndDisplayed()){
((JavascriptExecutor)driver).executeScript("arguments[0].checked = true", idYes.isElementPresentAndDisplayed());
idYes.click();}
或另一种尝试:(在本例中单选按钮未 selected)
if (idYes.isElementPresentAndDisplayed()){
((JavascriptExecutor)driver).executeScript("arguments[0].checked = true", idYes.isElementPresentAndDisplayed());
idYes.clickIfElement Present();}
但是当上述元素在不同场景下不可用时,则失败。
预期行为是:如果元素存在select或忽略。
将您的代码放在 try catch 块中。在 catch 块中,忽略发生的任何错误。
例如
try{
WebElement idYes = driver.findElement(By.id("idYes"));
if (idYes.isElementPresentAndDisplayed())
((JavascriptExecutor)driver).executeScript("arguments[0].checked = true",idYes.isElementPresentAndDisplayed());
idYes.click();
}catch(org.openqa.selenium.NoSuchElementException ignore){
//TODO: do nothing
}
希望对您有所帮助。
您也可以这样做:
WebDriverWait wait = new WebDriverWait(driver, 10); // Have a wait object with 10 sec(or whatever u prefer) to enforce on driver object stating for max time it should wait for the conditions
try{
wait.until(ExpectedConditions.elementToBeClickable(By.id("idYes"))).click(); // will explicitly wait for the element to be clickable, else throw timeout exception
}catch(TimeoutException toe){
System.out.println("Check box not present, hence skipping. " + toe); // here you can log or sysout the reason for skipping accompanied with exception
}