0

My program needs to find a series of Spans that their text starts with IMG. The table will potentially have multiple spans that meet this criteria. Example Formatting being:

<td>
  <span> IMG Sales Report </span>
</td>

I have tried several different lines of code, but i always end up with results being zero. Heres my latest attempt.

List<WebElement> files = driver.findElements(By.xpath("//span[starts-with(text(), 'IMG')]"));

Any help would be greatly appreciated, cause I cant find a solution that works.

Tevett Goad
  • 138
  • 1
  • 7

2 Answers2

0

this works for me.There is leading space in front of IMG.

//span[starts-with(normalize-space(.), 'IMG')]

Output

Output from xpath formatter

Amruta
  • 1,128
  • 1
  • 9
  • 19
0

To print the List of id attribute of the element you need to induce WebDriverWait for the visibilityOfAllElementsLocatedBy() and you can use Java8 stream() and map() and you can use either of the following Locator Strategies:

  • Using xpath and starts-with():

    List<String> myIDs = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//span[starts-with(., 'IMG')]")));
    
  • Using xpath and contains():

    List<String> myIDs = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//td/span[contains(., 'IMG')]")));
    

References

You can find a couple of relevant detailed discussions in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352