0

I am trying to locate a Button with a span text. I already tried it with the class but the class is used 6 times.

HTML:

<a href="#" class="btn-orange btn-border" ng-click="selectWorld(w.id)"> <span class="text world ng-binding">Krak des Chevaliers</span> </a>

C#:

driver.FindElement(By.XPath("(//a[span/text()='Krak des Chevaliers'])")).Click();

It can't find the Object. Please help :)

3
  • 1
    While I do not understand why your expression does not work, you could give a try to locate the span first and then get back to its parent element like this: //a/span[text()='Krak des Chevaliers']/.. Commented Dec 8, 2018 at 19:42
  • It still won't work. I also tried xpath (absolute and relative) and it also didn't work.
    – MP9
    Commented Dec 9, 2018 at 10:52
  • What about this expression: //*[text()='Krak des Chevaliers']/..? Commented Dec 9, 2018 at 16:49

1 Answer 1

0

Try this: By.xpath(“//*[contains(@class,’btn-orange’) and

.//*[contains(.,’Krak des Chevaliers’)]]”)

It’s probably worth opening the page manually and experimenting by opening the dev tools, hitting ctrl+F and pasting your attempt. No point getting selenium to spin up and fire all the other steps while you’re having locator troubles - if you can find it in the dev tools with that XPath but not in the test then it might be an unrelated timing issue

1
  • Thanks :) I already installed Chrompath and copied the xpath from there.
    – MP9
    Commented Dec 15, 2018 at 12:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.