Search code examples
javaseleniumxpathwebdriverwebdriverwait

How to check if located link contains a tag with a given text?


I need to check if located link contains <span class="extra-light"> with a given text. The way I locate the link:

ExpectedConditions.elementToBeClickable(By.xpath("//a[@so-extra=\"x12fog\"]"))

How to make it?


Solution

  • You can use findElements method with XPath locator defining the desired element as following:

    if(driver.findElements(By.xpath("//a[@so-extra='x12fog']//span[@class='extra-light' and contains(.,'stackoverflow')]")).size()>0){
        System.out.println("Element found");
    }
    

    findElements method return a list of found matching elements. So, if there is such element the list will be non-empty (size>0), otherwise the returned list will be empty.