I'm trying to use xPath to find an element containing a piece of text, but I can't get it to work....
WebElement searchItemByText = driver.findElement(By.xpath("//*[@id='popover-search']/div/div/ul/li[1]/a/span[contains(text()='Some text')]"));
If I remove the last bit with the "contains" thing, it locates my span element, but I need to select it based on the text contents. It's not gonna be a perfect match of 'Some text' either, because it might contain truncated strings as well.
Anyone see the issue?
This question is related to
selenium
xpath
selenium-webdriver
Use this
//*[@id='popover-search']/div/div/ul/li[1]/a/span[contains(text(),'Some text')]
OR
//*[@id='popover-search']/div/div/ul/li[1]/a/span[contains(.,'Some text')]
@FindBy(xpath = "//button[@class='btn btn-primary' and contains(text(), 'Submit')]") private WebElementFacade submitButton;
public void clickOnSubmitButton() {
submitButton.click();
}
@FindBy(xpath = "//span[@class='y2' and contains(text(), 'Your Text')] ")
private WebElementFacade emailLinkToVerifyAccount;
This approach will work for you, hopefully.
Source: Stackoverflow.com