0

So I'm trying to automate testing with selenium, and need to see if a check box is checked for a specific user. The page looks like this.

enter image description here

The first user's checkbox is not checked, and the second is. So, when checked there's a checkbox="checked" attribute that isn't there when not checked. I'm not sure how I would format it so that I'm able to specifically look at the box of the one that I'm looking for. I tried (//input[contains(@name='user_5166855' and @checked='checked')]) but it didn't seem to work.

Andersson
  • 51,635
  • 17
  • 77
  • 129
Devin
  • 3
  • 1
  • pls take the effort to make a reproduceable sample, also have a look into the [Q&A Guidelines](https://stackoverflow.com/help/how-to-ask) – LuckyLikey Nov 12 '18 at 16:06
  • Possible duplicate of [Selenium checkbox attribute "checked"](https://stackoverflow.com/questions/8187772/selenium-checkbox-attribute-checked) – JeffC Nov 12 '18 at 16:54

2 Answers2

0

Try below XPath to select

  • checked one

    //input[@name="user_5166855" and @checked]
    
  • unchecked

    //input[@name="user_5166855" and not(@checked)]
    

Note that checked is boolean attribute, so there is no need to verify the value of attribute - verifying existence should be enough

Andersson
  • 51,635
  • 17
  • 77
  • 129
  • You really should be using `.Selected` or `.isSelected()`, etc. and not trying to do this with locators. See the dup I linked. – JeffC Nov 12 '18 at 16:54
-1

So, you already have an Xpath for your second user all u need to do is:

    public boolean IsCheckBoxChecked()
{
    try
    {
    driver.findElement(By.name("//input[contains(@name='user_5166855' and @checked='checked')]")).getAttribute("checked")
    }
    catch
    {
    false
    }
}

So what u r doing here u find your element, in try block then your test is going to pass, if there is no such attribute for element(checkbox unchecked), then its will return false and assertion failed.

IPolnik
  • 619
  • 5
  • 13
  • You're trying to pass XPath (with invalid syntax) to `By.name` method. That definitely won't work. Also how do you know that OP uses Java? – Andersson Nov 12 '18 at 16:23