How to select the checkbox by passing the checkbox name in webdriver.

1,940 views
Skip to first unread message

Ramaguru N

unread,
Feb 20, 2013, 4:40:26 AM2/20/13
to seleniu...@googlegroups.com, webd...@googlegroups.com, chennai-testing
Hi,


If i pass the check box text from excel sheet, then corresponding checkbox should be selected in the application. 

How to do that? 

Thanks,
Ramaguru N

Ram krishna

unread,
Feb 20, 2013, 6:43:57 AM2/20/13
to webd...@googlegroups.com, seleniu...@googlegroups.com, chennai-testing
Hi Ramaguru,

I think you can do this by constructing the xpath related to the check box based on the name receiving from Excel sheet. Can you share the HTML source page or the website so that i can explain you here much better 

You have to write your own logic for constructing the Xpath (when you want to select particular check box based on the text from Excel)


Regards
Ramakrishna 

darrell

unread,
Feb 20, 2013, 7:42:19 AM2/20/13
to webdriver
It depends on whether or not the checkbox is using a known HTML
convention. If you look at http://www.w3schools.com/tags/tag_label.asp
you can see an example. Essentially, the text associated with the
input has a for attribute. If you find the label, get the for
attribute you can find the input. For example,

<label for='agreement'>I agree</label>
<input type='checkbox' id='agreement'>

If I get the text into the variable label and it is "I agree" the code
might look like:

String label = "I agree";
String labelLocator = By.xpath("//input[text()='" + label + "']");
WebElement labelElement = driver.findElement(labelLocator);
String checkboxID = labelElement.getAttribute("for");
Sting checkboxLocator = By.cssSelector("input#" + checkboxID);
WebElement checkboxElement = driver.findElement(checkboxLocator);

If they are not following this convention you would have to find the
input by using the XPath from the text to the input. For example, if
the above example is actually,

<span>I agree</span>
<input type='checkbox' id='agreement'>

then I would have to use:

String label = "I agree";
String checkboxLocator = By.xpath("//span[text='" + label + "']/../
input[@type='checkbox']");
WebElement checkboxElement = driver.findElement(checkboxLocator);

Although this looks shorter it is not as secure as the first example.
It also assumes there is only one check box parallel to the span.

Ramaguru N

unread,
Feb 20, 2013, 8:51:01 AM2/20/13
to webd...@googlegroups.com
Hi Ram,

Here is the HTML Code:

<tr>
<td>
<input id="chklist_general_areas_0" type="checkbox" value="1" name="ctl00$ContentPlaceHolderBody$chklist_general_areas$chklist_general_areas_0">
<label for="chklist_general_areas_0">Religion</label>
</td>
<td>
<input id="chklist_general_areas_5" type="checkbox" value="6" name="ctl00$ContentPlaceHolderBody$chklist_general_areas$chklist_general_areas_5">
<label for="chklist_general_areas_5">Politics</label>
</td>
</tr>
<tr>
<td>
<input id="chklist_general_areas_1" type="checkbox" value="2" name="ctl00$ContentPlaceHolderBody$chklist_general_areas$chklist_general_areas_1">
<label for="chklist_general_areas_1">Philosophy</label>
</td>


etc..


Regards,
Ramaguru N


--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To unsubscribe from this group and stop receiving emails from it, send an email to webdriver+...@googlegroups.com.
To post to this group, send email to webd...@googlegroups.com.
Visit this group at http://groups.google.com/group/webdriver?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.



Ram krishna

unread,
Feb 20, 2013, 9:01:17 AM2/20/13
to webd...@googlegroups.com
Hi Ramaguru,


Great,, Select the label that you want using xpath and then traverse to its parent using the 'following::parent' keyword in the xpath so that you can select the parent or corresponding Input ie., checkbox.... 

If it is little Ambiguous, Just share me the website so that i will spy with the firebug to construct the sample xpath and let you know so that you can create your own to work on it...


--
Thanks & Regards,
Ramakrishna.Telapolu,
9704620674.

darrell

unread,
Feb 21, 2013, 9:19:10 AM2/21/13
to webd...@googlegroups.com
They are following the convention of using the for attribute in the label tag. You can use:

public WebElement getCheckboxFromLabel(String label) {
    String labelLocator = By.xpath("//input[text()='"  + label + "']");
    WebElement labelElement = driver.findElement(labelLocator); 
    String checkboxID = labelElement.getAttribute("for"); 
    Sting checkboxLocator = By.cssSelector("input#" + checkboxID); 
    return checkboxElement = driver.findElement(checkboxLocator); 
}

WebElement religionCheckboxElement = getCheckboxFromLabel("Religion");
WebElement politicsCheckboxElement = getCheckboxFromLabel("Politics");
WebElement philosophyCheckboxElement = getCheckboxFromLabel("Philosophy");

Darrell

Maxim Vorobev

unread,
Feb 21, 2013, 10:27:02 AM2/21/13
to webd...@googlegroups.com
I think this can be simplified :) :
public WebElement getInputByLabel(String label) {
  String labelLocator = By.xpath("//input[@id=//label[text()='" + label + "']/@for]");
  return driver.findElement(labelLocator); 
}

Mark Collin

unread,
Feb 21, 2013, 10:29:52 AM2/21/13
to webd...@googlegroups.com
Looks like it will bomb out, findElement would expect to get an element in return.  You are returning the value of the @for attribute which is not a WebElement.

Maxim Vorobev

unread,
Feb 21, 2013, 10:44:04 AM2/21/13
to webd...@googlegroups.com
Nope. It's a valid xpath: //input[@id=//label[text()='sometext']/@for]
Search for input with id equals @for attribute from label note with given text

Mark Collin

unread,
Feb 21, 2013, 10:55:46 AM2/21/13
to webd...@googlegroups.com
You're right, I missed the final ] :)

darrell

unread,
Feb 25, 2013, 1:52:14 PM2/25/13
to webd...@googlegroups.com
Nice. Did some testing and it is notably faster than finding the label, getting the attribute and finding the input.

Darrell
Reply all
Reply to author
Forward
0 new messages