user5739042
user5739042

Reputation:

Unable to click element XPath

This is the element I'm trying to reach:

<div class="ui-dialog-buttonpane ui-widget-content ui-helper-clearfix">
  <div class="ui-dialog-buttonset">
    <button style="background-color: rgb(218, 218, 218);" aria-disabled="false" role="button" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" type="button">
        <span style="background-color: transparent;" class="ui-button-text">OK</span>
    </button>
  </div>
</div>

This is the code I'm using:

driver.FindElement(By.XPath("xpath=(//span[contains(@class,'ui-button-text')][contains(text(),'OK')]))")).Click();

I used the find element feature of the Selenium IDE using the xpath and it can find the element.

Upvotes: 0

Views: 637

Answers (3)

Diljith
Diljith

Reputation: 1

If you want the accurate solution: You can use this:

driver.FindElement(By.Xpath("//div[@class='ui-dialog-buttonset']/descendant::span[@class='ui-button-text' and contains(text(),'OK')]/parent::button")

Upvotes: 0

Gile Champion
Gile Champion

Reputation: 299

Other solution is to use this xpath:

driver.FindElement(By.XPath("///div[@class='ui-dialog-buttonset']/button/span")).Click();

Upvotes: 0

alecxe
alecxe

Reputation: 473833

You don't need the xpath= part inside the expression:

driver.FindElement(By.XPath("//span[contains(@class,'ui-button-text')][contains(text(),'OK')])")).Click();

Also, I think you can stop using contains() and check the complete class and text() values:

driver.FindElement(By.XPath("//span[@class = 'ui-button-text' and . = 'OK'])")).Click();

Here the . refers to the element's text.

Upvotes: 3

Related Questions