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

New contributor
MP9 is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
  • 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 at 19:42
  • It still won't work. I also tried xpath (absolute and relative) and it also didn't work. – MP9 2 days ago
  • What about this expression: //*[text()='Krak des Chevaliers']/..? – Bence Kaulics 2 days ago

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

New contributor
RossK is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

Your Answer

MP9 is a new contributor. Be nice, and check out our Code of Conduct.

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.