How to find inline li with input element inside it?

Given this HTML:

<li class="check_boxes input optional" id="activity_roles_input"> <fieldset class="choices"> <legend class="label"><label>Roles</label></legend> <input id="activity_roles_none" name="activity[role_ids][]" type="hidden" value="" /> <ol class="choices-group"> <li class="choice"> <label for="activity_role_ids_104"> <input id="activity_role_ids_104" name="activity[role_ids][]" type="checkbox" value="104" />Language Therapist </label> </li> <li class="choice"> <label for="activity_role_ids_103"> <input id="activity_role_ids_103" name="activity[role_ids][]" type="checkbox" value="103" />Speech Therapist </label> </li> </ol> </fieldset> </li> 

I am trying to use Selenium and xpath. I am trying to select the first link of the checkbox input element.
I have problems with selecting an item.
I cannot use the db identifier (104), as this is repeated for repeated tests with a new identifier each time. I need to select the "first" input check box, based on the fact that it has text for Language Therapist.

I tried:

 xpath=(//li[contains(@id,'activity_roles_input')])//input 

and

 xpath=(//li[contains(@id,'activity_roles_input')])//contains('Language Therapist") 

but he does not find the element.

When I do this:

 xpath=(//li[contains(@id,'activity_roles_input')]) 

he gets to the input set. The problem I am facing is to select the first checkbox for "Language Therapist".

+4
source share
4 answers

First find any <li> containing the text, and find in the descendant those that were for the first flag.

 xpath=(//li[contains(., "Language Therapist")]/descendant::input[@type="checkbox"][1]) 

(From Michael)

Worked above for me. In the end, I actually used

 xpath=(//li[contains(@id,'activity_roles_input')]/descendant::input[@type="checkbox"][1]) 

becuase I liked ID'ing by css id.

+9
source

You have

 xpath=(//li[contains(@id,'activity_roles_input')])//input 

Must not be

 xpath=(//li[contains(@id,'activity_roles_input')]//input) 

or rather

 xpath=(//li[@id='activity_roles_input']//input) 

?

0
source

An interesting fact to note when I try to run this small xsl against your xml.

XSL:

 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="text"/> <xsl:template match="/"> <xsl:for-each select="//li[@id ='activity_roles_input']"> <xsl:value-of select="."/> </xsl:for-each> </xsl:template> </xsl:stylesheet> 

Output:

  Roles Language Therapist Speech Therapist 
0
source
 xpath=(//li[@id='activity_roles_input']//input[1]) 
-1
source

All Articles