简体   繁体   English

如何判断 Selenium for Java 中是否选中了复选框?

[英]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();
}
  1. Declare a variable.声明一个变量。
  2. Store the checked property for the radio button.存储单选按钮的选中属性。
  3. Have a if condition.有一个 if 条件。

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.

相关问题 角材料-如何检查Selenium Java中是否选中了复选框? - Angular Material - How to check if checkbox is selected in selenium java? 你如何判断unicode字母是否在Java中是连续的? - How do you tell if unicode letters are sequential in Java? 如何在Java中使用Selenium Web驱动程序单击复选框? - How do I click on checkbox using selenium web driver in Java? 如何检查硒中是否选中了extjs复选框? - How to check if extjs checkbox is selected in selenium? 如何在Java中为复选框赋予一个值,例如“选择复选框时=加+ 20英镑”? - How do I give a value to my checkbox in java so for example, “when checkbox is selected = add + £20 total”? 如何使用Java Selenium启用Silverlight插件? - How do you enable Silverlight plugin using Java Selenium? 如何在Java中使用硒仅加载页面上的某些元素 - How do you load only certain elements on a page with selenium in Java 不使用.click()选中Selenium Java-mark复选框 - Selenium Java - mark checkbox selected without using .click() 无法验证选中的复选框,Selenium Webdriver Java - can't verify selected checkbox, Selenium Webdriver Java 即使未使用 Selenium Java 选中(选中)复选框,isSelected() 测试也会通过 - isSelected() test passes even if checkbox is not checked(selected) using Selenium Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM