使用selenium webdriver java
选择或不选中如何测试或验证复选框 package newpackage;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.WebElement;
//import org.openqa.selenium.firefox.*;
import java.util.concurrent.*;
public class Qemr { public static void main(String[]args){
System.setProperty("webdriver.gecko.driver","D:\\geckodriver-v0.10.0-win64\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
String baseUrl = "http://192.168.1.3:9091";
driver.get(baseUrl);
WebElement chkPersist = driver.findElement(By.name("remember"));
chkPersist=click();
for(int i=0;i<2;i++){
System.out.println("chkPersist.isChecked()");
}
}
}
答案 0 :(得分:3)
是的,有一些方法可以用于任何类型的控件,例如。对于复选框,您可以使用isSelected()方法,该方法返回布尔值,即0/1 适用于您的场景
package newpackage;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.WebElement;
//import org.openqa.selenium.firefox.*;
import java.util.concurrent.*;
public class Qemr {
public static void main(String[]args)
{
System.setProperty("webdriver.gecko.driver","D:\\geckodriver-v0.10.0-win64\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
String baseUrl = "http://192.168.1.3:9091";
driver.get(baseUrl);
WebElement chkPersist = driver.findElement(By.name("remember"));
//chkPersist.click();
if(chkPersist.isSelected())
{
System.out.println("Check box is Selected..");
}
}
}
答案 1 :(得分:0)
driver.findElement("").isSelected()
选项用于检查是否已选择Checkbox
。您可以在更新的代码中找到解决方案:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.WebElement;
//import org.openqa.selenium.firefox.*;
import java.util.concurrent.*;
public class StackOverFlow1
{
public static void main(String[]args)
{
System.setProperty("webdriver.gecko.driver","D:\\geckodriver-v0.10.0-win64\\geckodriver.exe");
WebDriver driver=new FirefoxDriver();
String baseUrl="http://192.168.1.3:9091";
driver.get(baseUrl);
boolean chkPersist=driver.findElement(By.name("remember")).isSelected();
if(chkPersist)
{
System.out.println("chkPersist is in selected state");
}
}
}
}
答案 2 :(得分:0)
问题:使用selenium webdriver java选择或不选中如何测试或验证复选框。
回答:是的我们可以使用isSelected()方法验证是否选择了webelement
package newpackage;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.WebElement;
import java.util.concurrent.*;
public class Qemr {
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver", "D:\\geckodriver-v0.10.0-win64\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
String baseUrl = "http://192.168.1.3:9091";
driver.get(baseUrl);
WebElement chkPersist = driver.findElement(By.name("remember"));
// Verify chkPersist element is Selected or Not ?
if (chkPersist.isSelected()) {
// if chkPersist element is selected then we print message
System.out.println("chkPersist element is already selected");
} else {
// if chkPersist element is not selected then we select / click on chkPersist element
chkPersist.click();
}
}
}