1

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...
Uday
  • 1,433
  • 10
  • 36
  • 57
  • What do you mean by checkbox names, do you want to get Countries printed? b'coz according to your HTML snippet I can see the name of the Checkbox is Countries. – Paras Aug 12 '14 at 09:37
  • Yes, my intent is country names printed. Once i get the solution for above my object is to print all the checkboxes which are selected. – Uday Aug 12 '14 at 09:39
  • I've provided the answer.. I am not sure whether you are looking for the same. – Paras Aug 12 '14 at 09:44
  • I badly need a solution for the above. Can somebody help me? – Uday Dec 17 '14 at 18:21

4 Answers4

1

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!

Sitam Jana
  • 3,123
  • 2
  • 21
  • 40
1

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.

user3657330
  • 158
  • 4
  • 11
0

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! :)

Paras
  • 3,197
  • 2
  • 20
  • 30
  • I know that i can get value i.e US and UK, but i want to get values associated to checkboxes as "United States" and "United Kingdon". How can i get those values? – Uday Aug 12 '14 at 09:46
  • The HTML which i gave is a sample HTML. What if we have more objects in the form? It will become tedious job to extract values. – Uday Aug 13 '14 at 04:25
  • Yea in that case it would become tedious.. but it is printing all the values at once, so if your purpose is just the name of the countries then it would do... I couldn't think of any other method by which you can fetch the name of the countries. – Paras Aug 13 '14 at 07:09
0

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

Community
  • 1
  • 1
Vignesh Paramasivam
  • 2,360
  • 5
  • 26
  • 57