Not able to select check box for dynamic input ID value

1k views Asked by At

Below is the html for two check boxes, and my intention is to select a checkbox using it's label as 'XERU' or 'SYM'. In Robot Framework, the keyword 'Checkbox Should Be Selected' uses Key attributes; ID and name as it's locator, but here ID is dynamic for each label.

So I need to frame an Xpath for the check box selecting it's ID / Name but not to be hard coded within the test suite.

<input id="ctl00_PageBody_chklistVirtualAddressingGroups_5" name="ctl00$PageBody$chklistVirtualAddressingGroups$5" type="checkbox"/>
<label for="ctl00_PageBody_chklistVirtualAddressingGroups_5">XERU</label>
<br/>
<input id="ctl00_PageBody_chklistVirtualAddressingGroups_6" name="ctl00$PageBody$chklistVirtualAddressingGroups$6" type="checkbox"/>
<label for="ctl00_PageBody_chklistVirtualAddressingGroups_6">SYM</label>
<br/>
3

There are 3 answers

1
Maninder On

You can try the following code. This code is based on HTML you provided. Please try to provide more information.

driver.findElement(By.xpath("//label[contains(text(), 'XERU')]")).click();
driver.findElement(By.xpath("//label[contains(text(), 'SYM')]")).click();
1
shicky On

I'd try something along these lines:

Wait Until Keyword Succeeds    4 sec    1 sec    Click Element    xpath=//label[contains(text(), 'XERU')]
Wait Until Keyword Succeeds    4 sec    1 sec    Click Element    xpath=//label[contains(text(), 'SYM')]

If this fails, can you give us further information as to why there's a problem?

0
Hemant Kulkarni On

Use xpaths as -

(//label[text()='XERU']/preceding::input)[last()]
(//label[text()='SYM']/preceding::input)[last()]