2

First, I want to check whether checkbox is selected or not and then have to perform select action. But here isselected method always giving false output, because the script is selecting the checkbox if it is not selected and deselecting if it is selected.

<div class="wrapper-tagline primary checkbox check-success">
<input id="checkbox" type="checkbox" checked="" value="1">
<label class="main-title section-option" for="checkbox">
<span>Show Section</span>
</label>
</div>
<div class="section-option-wrapper">

Any suggested solution?

  • 2
    Can you please add a snippet of your code and potentially the piece of HTML that you are attempting to work with. – Paul Muir Oct 7 '15 at 13:12
  • 1
    you should give the snippet of code, which will help use determine what is wrong. – demouser123 Oct 7 '15 at 15:16
  • Depending on how the html is coded, you need to provide us with that same HTML with the item checked and when it is unchecked. I am assuming that in the input tag checked is an empty string when unselected and a value of 1 when selected. Please tell me if it's checked in your above code. – DEnumber50 Oct 8 '15 at 21:47
1

I too faced the same problem. In my case I observed that the class name was changing on selecting/de-selecting the check box.For example the checkbox element when de-selected appears somewhat like this:

<input class="checkbox" id="current_year" name="current_year" type="checkbox" value="true">

and when it is checked/selected it appears lke this:

<input class="checkbox checked" id="current_year" name="current_year" type="checkbox" value="true">

I therefore used the class attribute in my xpath to determine if the checkbox is selected or not. For example to check if the checkbox is selected I used:

driver.findElement(By.xpath("//*[@id='current_year' and @class='checkbox checked']")).isDisplayed();

and to check if the checkbox is not selected I used:

driver.findElement(By.xpath("//*[@id='current_year' and @class='checkbox']")).isDisplayed();

You can check if any attribute in the checkbox element is changing on selecting it and can use the same to check if the checkbox is selected or not.

0
 boolean v=driver.findElement(By.xpath("//input[@type='checkbox']")).isSelected( );

Try the above code.

  • 4
    While this is certainly the way to use the above method, but we don't know why the OP is getting the false every time. Simply mentioning a one line code is not going to solve until you know why the code failed at the first place. – demouser123 Oct 12 '15 at 6:18
0

From the question I gues your syntax might by the problem. Instead of isselected() you must use isSelected() (S in selected is in uppercase).

You can also try using an alternative, for example, isChecked() or isActive() method instead of isSelected(). See if that works for you.

Try something like

boolean v=driver.findElement(By.xpath("the xpath here")).isChecked( );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.