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 :)

  • 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']/.. – Bence Kaulics Dec 8 '18 at 19:42
  • It still won't work. I also tried xpath (absolute and relative) and it also didn't work. – MP9 Dec 9 '18 at 10:52
  • What about this expression: //*[text()='Krak des Chevaliers']/..? – Bence Kaulics Dec 9 '18 at 16:49
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

  • Thanks :) I already installed Chrompath and copied the xpath from there. – MP9 Dec 15 '18 at 12:36

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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