Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Salesforce by (11.9k points)

This is the HTML I'm working with:

<div class="pbSubsection">
    <table class="detailList" cellspacing="0" cellpadding="0" border="0">
        <tbody>
            <tr>
                <td class="labelCol">
                    <label for="00Ni0000003bw8O">Becomes Asset</label>
                </td>
                <td class="dataCol col02">
                    <input id="00Ni0000003bw8O" type="checkbox" value="1" tabindex="8" name="00Ni0000003bw8O" checked="checked">
                </td>

I need to select the checkbox.

I cannot use the ID because I'm doing this across multiple orgs, and the ID is different in every org. Is there a way I can select by the label text or by the tabindex?

1 Answer

0 votes
by (32.1k points)
edited by

It is probably safer to get the checkbox by the label text rather than the tab index.

Solution 1 - :label locator:

If you are using watir-web driver, it possible to locate an element directly based on its label text - via the :label locator. You can do the following:

#Exact match of label text:

b.checkbox(:label => 'Becomes Asset').set

#Partial match of label text using regex:

b.checkbox(:label => /Asset/).set

Note that this assumes that the 'for' attribute of the label matches the 'id' attribute of the input element.

Solution 2 - Matching label to input:

For a solution that works in watir-classic (as well as watir-webdriver), you can:

  1. Get the label element by text
  2. Get for the attribute of the label element
  3. Get the associated checkbox by its id, which matches the label element's for attribute

The following will work for your page to clear the checkbox:

label = browser.label(:text => 'Becomes Asset')

browser.checkbox(:id => label.for).set

Check out the Salesforce Training course and enroll today! 

Browse Categories

...