Selenium, finding a span with specific text inside a specific div by its xpath?

12,395

As @Siking already correctly pointed out, you have a typo in your XPath. You probably need to add a slash (assuming the span element is inside the a element):

//div[@id='div1']/a/span[contains(text(), 'spanText')] 
             HERE^

Or, you can just look for the span anywhere inside the div:

//div[@id='div1']//span[contains(text(), 'spanText')] 
Share:
12,395
user1412716
Author by

user1412716

Updated on June 04, 2022

Comments

  • user1412716
    user1412716 almost 2 years

    I have a div, let's say it's id is div1, and I need to find a span inside that div, let's say it contains spanText as its text. How can I do this? I've tried the following but so far no luck.

    @FindBy(xpath = "//div[@id='div1']a/span[contains(text(), 'spanText')]")
    

    What am I missing?