gpt4 book ai didi

java - 获取隐藏元素的属性

转载 作者:行者123 更新时间:2023-11-28 20:32:20 24 4
gpt4 key购买 nike

我有一个要测试的表。其中一列内是是/否单选按钮,当页面呈现时隐藏在图像下方,以便在选择是或否时有一个复选标记或一个 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);

与空指针异常错误类似。

最佳答案

您的代码没有任何意义。为了获得不可见元素的 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);

关于java - 获取隐藏元素的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14655881/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com