0

Is it possible to write xpath using contains text such as(Below is what I want but does not work)

//ul[@role='listbox']/..//span[contains(text(),'Fast-Food Restaurent')] 

Page Code:

<span class="item-title" md-highlight-text="searchText" md-highlight-flags="i">Fast-<span class="highlight">Food</span> Restaurant</span>

It is an auto complete text box when I enter the word food, there are some options and I want to select Fast-Food Restaurant from it.

Thanks in advance.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Sheikh Rahman
  • 895
  • 3
  • 14
  • 29

3 Answers3

0

You can select required span node based on its string representation:

//span[.='Fast-Food Restaurant']
JaSON
  • 4,843
  • 2
  • 8
  • 15
0

To identify the element you can use either of the following based Locator Strategies:

  • Using the texts Food and Fast:

    //ul[@role='listbox']/..//span[.//span[text()='Food']][contains(.,'Fast-')]
    
  • Using the texts Food and Restaurant:

    //ul[@role='listbox']/..//span[.//span[text()='Food']][contains(.,'Restaurant')]
    
  • Using the texts Food, Fast and Restaurant:

    //ul[@role='listbox']/..//span[.//span[text()='Food']][contains(.,'Fast-') and contains(.,'Restaurant')]
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

You can amend your path to the following:

//ul[@role='listbox']/..//span[contains(normalize-space(), 'Fast-Food Restaurant')] 
Alexey R.
  • 8,057
  • 2
  • 11
  • 27