[英]How do you tell if a checkbox is selected in Selenium for Java?
I am using Selenium in Java to test the checking of a checkbox in a webapp.我在 Java 中使用Selenium来测试 webapp 中复选框的检查。 Here's the code:
这是代码:
private boolean isChecked;
private WebElement e;
I declare e
and assign it to the area where the checkbox is.我声明
e
并将其分配给复选框所在的区域。
isChecked = e.findElement(By.tagName("input")).getAttribute("checked").equals("true");
What is weird is that getAttribute("checked")
returns null
and therefore a NullPointerException
奇怪的是
getAttribute("checked")
返回null
,因此返回NullPointerException
In the HTML for the checkbox, there is no checked
attribute displayed.在复选框的 HTML 中,没有显示已
checked
属性。 However, isn't it the case that all input
elements have a checked = "true"
so this code should work?但是,是不是所有
input
元素都有一个checked = "true"
所以这段代码应该可以工作?
If you are using Webdriver then the item you are looking for is Selected.如果您使用的是 Webdriver,那么您要查找的项目是 Selected。
Often times in the render of the checkbox doesn't actually apply the attribute checked unless specified.除非指定,否则在复选框的渲染中通常不会实际应用选中的属性。
So what you would look for in Selenium Webdriver is this所以你会在 Selenium Webdriver 中寻找的是这个
isChecked = e.findElement(By.tagName("input")).Selected;
As there is no Selected in WebDriver Java API, the above code should be as follows:由于WebDriver Java API中没有Selected,所以上面的代码应该是这样的:
isChecked = e.findElement(By.tagName("input")).isSelected();
if ( !driver.findElement(By.id("idOfTheElement")).isSelected() )
{
driver.findElement(By.id("idOfTheElement")).click();
}
if(checkBox.getAttribute("checked") != null) // if Checked
checkBox.click(); //to Uncheck it
You can also add an and
statement to be sure if checked is true.您还可以添加
and
语句以确保检查是否为真。
I would do it with cssSelector:我会用 cssSelector 来做:
// for all checked checkboxes
driver.findElements(By.cssSelector("input:checked[type='checkbox']"));
// for all notchecked checkboxes
driver.findElements(By.cssSelector("input:not(:checked)[type='checkbox']"));
Maybe that also helps ;-)也许这也有帮助;-)
For the event where there are multiple check-boxes from which you'd like to select/deselect only a few, the following work with the Chrome Driver (somehow failed for IE Driver):对于有多个复选框的事件,您只想从中选择/取消选择几个,以下适用于 Chrome 驱动程序(IE 驱动程序以某种方式失败):
NOTE: My check-boxes didn't have an ID associated with them, which would be the best way to identify them according to the Documentation.注意:我的复选框没有与它们关联的 ID,这将是根据文档识别它们的最佳方式。 Note the
!
注意
!
sign at the beginning of the statement.在声明的开头签名。
if(!driver.findElement(By.xpath("//input[@type='checkbox' and @name='<name>']")).isSelected())
{
driver.findElement(By.xpath("//input[@type='checkbox' and @name= '<name>']")).click();
}
Lets assume让我们假设
private string isChecked;
private webElement e;
isChecked =e.findElement(By.tagName("input")).getAttribute("checked");
if(isChecked=="true")
{
}
else
{
}
Hope this answer will be help for you.希望这个回答对你有所帮助。 Let me know, if have any clarification in CSharp Selenium web driver.
如果在 CSharp Selenium Web 驱动程序中有任何说明,请告诉我。
public boolean getcheckboxvalue(String element)
{
WebElement webElement=driver.findElement(By.xpath(element));
return webElement.isSelected();
}
The mechanism of selenium framework: selenium框架的机制:
Here selenium make request to the its server and fetch first subelement with tagname input这里 selenium 向其服务器发出请求并获取带有标记名输入的第一个子元素
WebElement e = e.findElement(By.tagName("input"));
Than you try to receive attribute on that element比您尝试接收该元素上的属性
object attribute = e.getAttribute("checked")
So either use所以要么使用
findElement(By.attribute("checked")
or use或使用
findElement(By.xpath("\\input[@checked='true']")
PS I'm not familiar with java's equivalent of selenium api so some method may be named slightly different. PS 我不熟悉 java 的 selenium api 等价物,因此某些方法的名称可能略有不同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.