Uday
Uday

Reputation: 1484

How to get the text associated with a checkbox in Java

I have below html, how can i get the chechbox names?

<html>
<body>
<form>
<input type="checkbox" name="Countries" value="US">Unites States</input>
<input type="checkbox" name="Countries" value="UK">United Kingdom</input>
</form>
</body>
</html>

I tried below, but none helps:

List<WebElement> eles=driver.findElements(By.name("Countries"));
Integer ddSize=eles.size();
for(Integer i=0;i<ddSize;i++)
        System.out.println(eles.get(i).getText());

or
for(WebElement ele:eles)
        System.out.println(ele.getText());
also tried ele.getAttribute("text") etc...

Upvotes: 1

Views: 9580

Answers (4)

Vignesh Paramasivam
Vignesh Paramasivam

Reputation: 2470

Selenium Webdriver treats innerHTML as an attribute, so you could use

   String text = ele.getAttribute("innerHTML");

moreover, the getText() will not work well with the input element

This post will explain you about RC too here

Edit: as per OP comments

innerHTML will work well with most of the browsers. Or you can use javascript executor to do it.

 String text = (String)(JavascriptExecutor (driver)).executeScript("return arguments[0].innerHTML;", ele); 

You can look at this post for more insights

Upvotes: 0

Paras
Paras

Reputation: 3235

Please use the below code to get the value of Checkboxes.

List<WebElement> eles=driver.findElements(By.name("Countries"));
    Integer ddSize=eles.size();
    for(Integer i=0;i<ddSize;i++)
            System.out.println(eles.get(i).getAttribute("value"));

I've modified your code instead of getting the text value you need to use getAttribute("value") method with attribute as value.

Your output would be something like:

US
UK

If you just want to print the values you can also do that by using below line of command.

System.out.println(driver.findElement(By.xpath("html/body/form")).getText());

This will give output as mentioned below, b'coz your text is tagged with form tag not with input tag:

Unites States United Kingdom

hope it helps! :)

Upvotes: 0

user3657330
user3657330

Reputation: 158

There was an issue raised long time back - https://code.google.com/p/selenium/issues/detail?id=2922

Quoting from that issue -

Closing as fixed, it is decided long ago that getText returns empty string for input elements, a user should use getAttribute("value").

Your html code already has a "value" inside your input tag making things a bit hard though.

According to w3schools regarding usage of "value" attribute -

For "checkbox", "radio", "image" - it defines the value associated with the input (this is also the value that is sent on submit)

So, I believe there won't be a need to include an extra text (like the United States and United Kingdom in your code) for an "input" element with "checkbox" as this is in standard practice taken care of, by using the "value" attribute for the checkbox.

Upvotes: 1

Sitam Jana
Sitam Jana

Reputation: 3129

You can write a code something like below:

List<WebElement> checkboxes=driver.findElements(By.xpath("//input[@type='checkbox']"));
for (WebElement checkbox: checkboxes) {
   System.out.println(checkbox.getText());

Hope it helps!

Upvotes: 1

Related Questions