我有一张桌子,我试图测试。其中一列是yes / no单选按钮,当页面被渲染时,它隐藏在图像下方,以便在选择是或否时有复选标记或X.
选择“是”时,单选按钮的属性为checked="true"
。
如何使用Selenium2
获取该属性,以便我可以对其进行比较或对其进行断言?
我尝试了以下操作,这只会导致空指针错误。
private void verifyPOEligibleYesNull(String driverName)
{
int min = 1;
int max = 6;
JavascriptExecutor js = (JavascriptExecutor)driver;
StringBuilder stringBuilder = new StringBuilder();
//Navigate to System Facing
driver = browser.getDriver(driverName);
driver.manage().window().maximize();
driver.navigate().to("http://theURL");
//Enter userName
WebElement userName = driver.findElement(By.id("username"));
userName.clear();
userName.sendKeys(user);
//Enter Password
WebElement password = driver.findElement(By.id("pword"));
password.clear();
password.sendKeys(pwd);
//Click Login
WebElement loginBtn = driver.findElement(By.xpath("/html/body/div/div/form/div[3]/div/input"));
loginBtn.click();
try
{
Thread.sleep(5000);
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
WebElement ivToPoBtn = driver.findElement(By.xpath("/html/body/div/div/div/ul/li/a/img"));
ivToPoBtn.click();
try
{
Thread.sleep(5000);
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
//Grab a random row and determine whether the PO Eligible is "Yes"
String rowNum = Integer.toString(rand.nextInt(max - min + 1) + min);
WebElement randomRow = driver.findElement(By.xpath("/html/body/div/div/div/div/form/div/div[2]/div/div[2]/div/div/div/div/div/div/div/table/tbody/tr["+rowNum+"]/td/div/div/input"));
String poEligibleChecked = randomRow.getAttribute("checked").toString();
stringBuilder.append("var x = $(\""+poEligibleChecked+"\");");
stringBuilder.append("return x.toString();");
String res = (String) js.executeScript(stringBuilder.toString());
System.out.println(res);
}
我甚至尝试过从WebDriver FAQ建议的以下内容:
WebElement randomRow = driver.findElement(By.xpath("/html/body/div/div/div/div/form/div/div[2]/div/div[2]/div/div/div/div/div/div/div/table/tbody/tr["+rowNum+"]/td/div/div/input"));
String poEligibleChecked = randomRow.getAttribute("checked");
String element = js.executeScript("return arguments[0].getText();", poEligibleChecked).toString();
System.out.println(element);
具有类似的空指针异常错误。
答案 0 :(得分:2)
你的代码真的没有意义。要获取不可见元素的checked
属性,只需使用element.getAttribute()
:
final String scriptGetChecked = "return arguments[0].getAttribute('checked')";
WebElement randomRow = driver.findElement(By.whatever("something"));
String checkedState = (String) js.executeScript(scriptGetChecked, randomRow);