0

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.

  • Have you tried using the browser developer tools to select the xpath? – Scary Wombat Jul 11 '19 at 01:31
  • check if the span exist in another window, as it's saying `popup` in the class name. – supputuri Jul 11 '19 at 02:09
  • The span does not exist in another window. I tried using the browser development tool in Chrome. I haven't found a good resource. They all just say search for xpath in Elements pane1 or $() in console panel. Nothing comes back for either. – I.A. Washburn Jul 11 '19 at 02:17
  • 1
    right click on the element and hit `Inspect` in the context menu (last option), after opening the dev tools. That should point you to the element. Then try the xpath as shown in [here](https://stackoverflow.com/questions/55870609/is-there-a-way-to-learn-xpath-without-using-firebug-or-xpath-as-firefox-is-not-s/55870909#55870909) – supputuri Jul 11 '19 at 02:27
  • how about Browser.findElement(By.xpath("//span[contains(text(), 'Yes')]")); – Jeremy Kahan Jul 11 '19 at 02:28
  • @supputuri That worked. Thank you! – I.A. Washburn Jul 11 '19 at 02:43

2 Answers2

1

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']")));
    
Dmitri T
  • 159,985
  • 5
  • 83
  • 133
0

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

YoDO
  • 93
  • 1
  • 7