I.A. Washburn
I.A. Washburn

Reputation: 39

Trying to find text element in a span

I am coding a selenium webdriver test in Java. I need the test to click on a "Yes" button in chrome that does not have an id or name. I cannot find the element to click on the button that only has "Yes" as its unique identifier. There is also a "No" button.

I have tried to find the WebElement using xpath, classname and I have tried findElements function. Nothing succeeds.

This is the HTML:

<span class="ui-btn-inner">
<span class="ui-btn-text popup-anchor-text">Yes</span>
</span>

I have tried:

WebElement yesBtn = browser.findElement(By.xpath("//div[@class='ui-btn-text popup-anchor-text']/span"));

WebElement yesBtn = browser.findElement(By.xpath("//span[.='Yes']"));

WebElement yesBtn = browser.findElement(By.xpath("//div[contains(text(), 'Yes')]"));

WebElement yesBtn = browser.findElement(By.xpath("//button[@class='ui-btn-text popup-anchor-text' and span='Yes']"));

WebElement yesBtn = browser.findElement(By.xpath("//div[@class='ui-btn-text popup-anchor-text' and span='Yes']"));

yesBtn.click();

List<WebElement> yesBtn = browser.findElements(By.className("ui-btn ui-shadow ui-btn-corner-all ui-btn-up-a"));

yesBtn.get(0).click();

Error message: NoSuchElementException; no such element. Unable to locate element.

Upvotes: 0

Views: 1691

Answers (2)

YoDO
YoDO

Reputation: 105

Try out //span[@class='ui-btn-inner']/descendant::span[contains(text(), 'Yes')]. It should help out.

Upvotes: 0

Dmitri T
Dmitri T

Reputation: 168217

The correct XPath locator would be:

//span[text()='Yes']

Just in case you can go for normalize-space() function:

//span[normalize-space()='Yes']

If this doesn't help:

  1. Make sure that the span doesn't belong to an iframe, otherwise you will have to switch to the iframe before attempting to locate the element.

    driver.switchTo().frame("your-frame");
    
  2. Make sure to use WebDriverWait class just in case the element is not immediately available so WebDriver would perform several find attempts with polling interval

    new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[text()='Yes']")));
    

Upvotes: 1

Related Questions