1

My page looks like:

<div class="tabBox">
<table id="ctl00_ctl00_MainContentPlaceHolder_MainContentPlaceHolderPM_lstAndroidDeviceType">
    <tbody><tr>
        <td><input id="ctl00_ctl00_MainContentPlaceHolder_MainContentPlaceHolderPM_lstAndroidDeviceType_0" type="checkbox" name="ctl00$ctl00$MainContentPlaceHolder$MainContentPlaceHolderPM$lstAndroidDeviceType$0" value="10"><label for="ctl00_ctl00_MainContentPlaceHolder_MainContentPlaceHolderPM_lstAndroidDeviceType_0">1</label></td>
    </tr><tr>
        <td><input id="ctl00_ctl00_MainContentPlaceHolder_MainContentPlaceHolderPM_lstAndroidDeviceType_1" type="checkbox" name="ctl00$ctl00$MainContentPlaceHolder$MainContentPlaceHolderPM$lstAndroidDeviceType$1" value="17"><label for="ctl00_ctl00_MainContentPlaceHolder_MainContentPlaceHolderPM_lstAndroidDeviceType_1">2222222</label></td>
    </tr><tr>
        <td><input id="ctl00_ctl00_MainContentPlaceHolder_MainContentPlaceHolderPM_lstAndroidDeviceType_2" type="checkbox" name="ctl00$ctl00$MainContentPlaceHolder$MainContentPlaceHolderPM$lstAndroidDeviceType$2" value="6"><label for="ctl00_ctl00_MainContentPlaceHolder_MainContentPlaceHolderPM_lstAndroidDeviceType_2">2323</label></td>
    </tr><tr>
        <td><input id="ctl00_ctl00_MainContentPlaceHolder_MainContentPlaceHolderPM_lstAndroidDeviceType_3" type="checkbox" name="ctl00$ctl00$MainContentPlaceHolder$MainContentPlaceHolderPM$lstAndroidDeviceType$3" value="7"><label for="ctl00_ctl00_MainContentPlaceHolder_MainContentPlaceHolderPM_lstAndroidDeviceType_3">232323</label></td>
    </tr><tr>
        <td><input id="ctl00_ctl00_MainContentPlaceHolder_MainContentPlaceHolderPM_lstAndroidDeviceType_4" type="checkbox" name="ctl00$ctl00$MainContentPlaceHolder$MainContentPlaceHolderPM$lstAndroidDeviceType$4" value="12"><label for="ctl00_ctl00_MainContentPlaceHolder_MainContentPlaceHolderPM_lstAndroidDeviceType_4">4</label></td>
    </tr><tr>

</tbody></table>
                                                </div>

enter image description here

My goal is to create a method that will take a String and and as a result click the corresponding checkbox.

public void toggleDeviceType(String name) {}

toggleDeviceType("nameOfDevice")

How can I select the checkboxes in this div by passing in a name via a String?

note: whats the best way to handle multiple selection here? I don't want the user writing a test outside the framework to be creating an array and adding to it themselves, would rather take care of it in the framework itself

  • Can you add the code you have now and where you are currently stuck? – Chris Kenst Mar 7 '17 at 18:49
  • I am currently just clicking the WebElement based on a hard coded id currently while I pad out the rest of the functionality for this page, I thought I would ask for guidance on this part as its the only part I will struggle to figure out. driver.findElement(By.id("ctl00_ctl00_MainContentPlaceHolder_MainContentPlaceHolderPM_lstAndroidDeviceType_4").click(); as a temporary solution to get a successful add and work on some other functionality – symon Mar 7 '17 at 18:58
1

You can use xquery which is xpath, but the string is formatted differently. The specify the text to look for in that. driver.findElement(By.xpath("//div[@class='tabBox']//input[@value='string'])) Depending on your control type this should allow you to pass in the 'string' part as a variable to find the specific input box you are looking for. You can then call the .click(); method once you find the specific checkbox.

Optionally you can also read in the input control itself and then select the corresponding value. However, your html looks more like it's a table with separate inputs for each row instead of a full control, which is why I indicated the specific input target instead of the other way.

1

The solution I used in the end was:

    public void disableMultipleDevices(String[] device) {   
    for (String s : device) {
    WebElement ElementByName = driver.findElement(By.xpath("//label[text()='"+s+"']/preceding-sibling::input[@type='checkbox']"));
    if (ElementByName.isSelected()) {
        ElementByName.click();
    }           
    }
}

Note to anybody who may come across this in future, naively I didn't even consider calling .click(); on the label element, this would of solved my problem much easier, so I would urge you to try that out first in a similar situation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.