1

I'm trying to get the text value of checkbox label. Here is an HTML code

<div id="content" class="large-12 columns">
        <div class="example">
  <h3>Checkboxes</h3>
  <form id='checkboxes'>
    <input type="checkbox"> checkbox 1</br>
    <input type="checkbox" checked> checkbox 2
  </form>
</div>

So What I have tried so far

  1. Driver.FindElement(By.XPath("//input[@type='checkbox']")).Text;
  2. Driver.FindElement(By.XPath("//input[@type='checkbox']")).GetAttribute("value");
  3. Driver.FindElement(By.XPath("//input[@type='checkbox']")).GetAttribute("name");
  4. Driver.FindElement(By.XPath("//input[@type='checkbox']")).GetAttribute("innerText");
  5. Driver.FindElement(By.XPath("//input[@type='checkbox']")).GetAttribute("innerHTML");

Here is a screenshot

All this attempts return "". Any ideas how to get it or Javascript is my only option ?

2
  • there are two check boxes, which one you want? Commented Aug 12, 2018 at 23:51
  • Either one of them, but findelement should return first element from all elements. And see them but property Text has value ` "" ` . Commented Aug 13, 2018 at 2:27

4 Answers 4

1

The xpath for a text following an input tag is //input[1]/following-sibling::text()[1] but there are serious limitations for Selenium to run expressions like this. It only can handle tag elements. Try to get the parent and retrieve texts from there.

string[] texts = Driver.FindElement(By.XPath("//form[@id='checkboxes']"))
   .GetAttribute("innerText")
   .Split("\r\n".ToCharArray()
);

Then texts[0] returns:

checkbox 1

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot it. Really worked. I got it that the checkbox label a hard coded into a form. I believe this is not the smartest idea to design your page this way but at least I know a solution right now Screenshot of what have driver returned
0

Add an class or id attribute to the checkboxes and then try to find the element by css selector like: Driver.FindElement(By.CssSelector(""))

1 Comment

The issue is not that I can't find it or it is not visible, but that I can't return a text value of it. link
0

You can try following to get the required text from the two checkboxes:

Driver.FindElement(By.XPath("//form[@id='checkboxes']/input[@type='checkbox'][1]")).Text
Driver.FindElement(By.XPath("//form[@id='checkboxes']/input[@type='checkbox'][2]")).Text

Let me know, if above code does not work for you.

3 Comments

Thanks. I tried this option. You can see uploaded screenshot
Could you retry with //form[@id='checkboxes']/input[@type='checkbox'][1] as the xpath for checkbox, as currently I am not sure whether the checkbox //input[@type='checkbox'][1] is one the children of //form[@id='checkboxes'] or some other one.
See a result in a screenshot Didn't work. The final answer is posted by @derloopkat . I think this is valuable informatio
0

You can also use CSS Selector to get the checkbox label:

Element CheckBox = Driver.FindElement(By.CssSelector("input[type='checkbox']"));

string firstChkBoxTxt = CheckBox.FirstOrDefault().GetAttribute("innerText");
string secondChkBoxTxt = CheckBox.LastOrDefault().GetAttribute("innerText");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.