Deepak Shah
Deepak Shah

Reputation: 175

checking Check box in Selenium

Testing scenario goes like this:

I create some users eg. User EF, User GH, etc.

Now I need to select these Users from list of Users (Users are sorted alphabetically so newly created users are not always at top or bottom, depending on user name, they can be in between)

Now my developer is using unOrder list to display list of users.

HTML Code as under:

<DIV class="reflex-ssl-result-list "> 
<UL> 
<LI><INPUT value="" type=checkbox><LABEL class=reflex-search-result-title></LABEL> 
<LI><INPUT value="" type=checkbox><LABEL class=reflex-search-result-title>A</LABEL> 
<LI><INPUT value="" type=checkbox><LABEL class=reflex-search-result-title>B</LABEL> 
<LI><INPUT value="" type=checkbox><LABEL class=reflex-search-result-title>C</LABEL> 
<LI><INPUT value="" type=checkbox><LABEL class=reflex-search-result-title>D</LABEL> 
<LI><INPUT value="" type=checkbox><LABEL class=reflex-search-result-title>E</LABEL> 
<LI><INPUT value="" type=checkbox><LABEL class=reflex-search-result-title>F</LABEL> 
<LI><INPUT value="" type=checkbox><LABEL class=reflex-search-result-title>G</LABEL> 
</UL> 
</DIV>

C and D are the newly created user and that is the one I need to select. How can I do this? I have tried xpath, but the issue is position of newly created user can be anywhere. If I was selecting user in the same location all the time, xpath is good for me, but in my scenario, I need something more than xpath.

Any suggestions?

Upvotes: 0

Views: 529

Answers (4)

user3487861
user3487861

Reputation: 350

XPaths:

E -- //label[text()='E']/preceding-sibling::input

F -- //label[text()='F']/preceding-sibling::input

G -- //label[text()='G']/preceding-sibling::input

H -- //label[text()='H']/preceding-sibling::input

Like Above XPaths You can click on the any Checkboxes to create combination of users

Please Let me know is my funda worked or not ?

Upvotes: 0

Mai Hanafy
Mai Hanafy

Reputation: 91

try this

<tr>
    <td>storeXpathCount</td>
    <td>//input[@name='NAME']</td>
    <td>total</td>
</tr>
<tr>
    <td>storeEval</td>
    <td>Math.floor(Math.random() * ${total} )+1</td>
    <td>index</td>
</tr>
<tr>
    <td>click</td>
    <td>xpath=(//input[@id='NAME'])[${index}]</td>
    <td></td>
</tr>

Upvotes: 0

Ajay
Ajay

Reputation: 395

try this xpath

//label[contains(text(),'newly created user name')]

example:if you create user A

driver.findElement(By.xpath("//label[contains(text(),'A')]")).click();

so where ever may be your user is created,you can click based on text and based on HTML tag.

Hope this helps you

-Aj

Upvotes: 0

Faiz
Faiz

Reputation: 3256

Use XPath to identify the label containing the text (eg: 'D'), go up one level to it's parent (the LI), and then select the input within.

//LABEL[.='D' and @class='reflex-search-result-title']/../INPUT

Since element tags in your example are in upper case, and Xpath case sensitivity can be an issue sometimes, check How XPath Works in WebDriver to see what applies to you.

Upvotes: 2

Related Questions