1

How can I print label associated with <input type="checkbox" />, like checkbox 1 or checkbox 2 in example below.

Checkbox Items:

<input type="checkbox" value="cb1" name="checkboxes[]">Checkbox 1 
<input type="checkbox" value="cb2" name="checkboxes[]">Checkbox 2 
<input type="checkbox" checked="checked" value="cb3" name="checkboxes[]">Checkbox 3

I am using below code to get the label name , but it doesn't work. getText() is also not working.

List<WebElement>  elementList = driver.findElements(By.name("checkboxes[]"));

System.out.println("checkbox1?  "+elementList.get(0).getAttribute("innerHTML"));`

1 Answer 1

3

HTML input tags are void elements & they do not need a closing tag and they are not designed for holding any text / other elements inside. Refernce .

So, getText() / getAttribute("innerHTML") on the 'input' tag element will not work. Find the parent element of the input tag, then find the innerText/innerHTML and parse it to get the text you want!.

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

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.