0

I am learning on the xpath philosophy. Most of the examples found are, in a way, standard and finding xpath manually could be easy. In the following HTML code, I am not able to understand how should I find the xpath of Job Duration and then create a list of elements for li items.

<div class="box rounded6">
   <h3 class="s_filter_title">Job Duration :</h3>
   <ul>
      <li><label><input type="checkbox" class="" name="">Contract</label></li>
      <li><label><input type="checkbox" class="" name="">Full Time </label></li>
      <li><label><input type="checkbox" class="" name="">Part Time </label></li>
      <li><label><input type="checkbox" class="" name="">Internship</label></li>
      <li><label><input type="checkbox" class="" name="">Temporary</label></li>
      <li><label><input type="checkbox" class="" name="">Temp To Perm</label></li>
   </ul>
</div>
ViTest
  • 29
  • 6

2 Answers2

3

Here is the xpath that you are looking for.

//h3[.='Job Duration :']/following-sibling::ul/li//input

There are 6 li elements in the ul, you can see the count as 6 in the search list.

enter image description here

supputuri
  • 13,644
  • 2
  • 21
  • 39
  • Thanks @supputuri. I was able to loop thru the list and print the text of each checkbox. When I try to click on the element from the list, e.click(), it doesn't select any of the check boxes. Any thoughts, please? – ViTest Apr 30 '19 at 10:43
  • if you want to select the checkbox then you have to add `input` to the xpath, updated the xpath in the answer try with that. – supputuri Apr 30 '19 at 12:24
  • Thanks @supputuri. I am able to select the check boxes. – ViTest May 09 '19 at 10:14
0

To create a List of the <li> elements associated with the element with text as Job Duration you need to identify the element with text as Job Duration first and then locate the following <ul> node and then locate it's child nodes and you can use the following Locator Strategy:

  • Java based solution:

    • xpath1:

      List<WebElement> myElements = driver.findElements(By.xpath("//h3[text()='Job Duration :']//following::ul[1]//li"));
      
    • xpath2:

      List<WebElement> myElements = driver.findElements(By.xpath("//h3[contains(., 'Job Duration')]//following::ul[1]//li"));
      
  • Python based solution:

    • xpath1:

      my_elements = driver.find_elements_by_xpath("//h3[text()='Job Duration :']//following::ul[1]//li")
      
    • xpath2:

      my_elements = driver.find_elements_by_xpath("//h3[[contains(., 'Job Duration')]//following::ul[1]//li")
      
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352