3

I'm trying to grab a span element that has text " collections". I attach a screenshot of the HTML code. I need to get this element not using the class values but just the text. This is what I tried without any luck. Notice that there's an empty space before the word 'colelctions/'

//*[contains(text(),' collections')]

It seems a pretty simple thing to do but somehow it doesn't work. Any suggestions? Many thanksenter image description here

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Angelo
  • 1,594
  • 5
  • 17
  • 50

2 Answers2

3

Try to use below instead:

//*[contains(.,' collections')]

or

//*[contains(text()[2],' collections')]

Here is a post about difference between . and text()

yong
  • 13,357
  • 1
  • 16
  • 27
Andersson
  • 51,635
  • 17
  • 77
  • 129
  • thank you, the first option works. May I ask how come my solution above doesn't work? What am I doing wrong? Thanks – Angelo Aug 13 '18 at 12:19
  • I guess this is because `//*[contains(text(),' collections')]` is trying to evaluate the text of the first text node. Note that `span` node contains 2 child text nodes and the first one is just a space/new line character... – Andersson Aug 13 '18 at 12:52
  • you're totally right. I recently found https://stackoverflow.com/questions/9493732/difference-between-text-and-string and key take away was the presence of empty spaces in the string value that was probably not causing my code to work. Thank you so much for your answers, much appreciated. – Angelo Aug 13 '18 at 18:05
0

To grab the element with text as collections as the element is an Angular element you have to induce WebDriverWait and you can use the following solution:

element = driver.find_element_by_xpath("//span[normalize-space()='collections']")
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • thank you. Please, one quick question. How can you tell from the code it's angular element? – Angelo Aug 13 '18 at 12:18