2

How can I click on this element?

<a class="_eszkz _l9yih" href="#" role="button" aria-disabled="false">
<span class="_8scx2 coreSpriteHeartOpen">XYZ</span></a>

What must be in my variable to make it work?

string element = "???"

driver.FindElement(By.XPath(element)).Click();
2
  • you can try this XPATH -> element = '//a/span[contains(text(), "XYZ")]' Commented Nov 12, 2017 at 13:15
  • @RehanShikkalgar - It does not work! Commented Nov 12, 2017 at 13:35

1 Answer 1

3

There are a lot of possibilities for you to identify the element you want to click. You just have to make sure you pick some attribute or value that provides the exact element you want to click, and not some other element with the same class name for example.

So you have to determine how to identify the correct example

By its own class name? Use:

By.XPath("//a[@class='_eszkz _l9yih']")

By the class of its child? Use:

By.XPath("//span[@class='_8scx2 coreSpriteHeartOpen']/..")

By the text contents of its child? Use:

By.XPath("//span[contains(., 'XYZ')]")

You can also store the XPath in a variable of type By, so instead of using:

string element = "//a[@class='_eszkz _l9yih']";

you can use

By element = By.XPath("//a[@class='_eszkz _l9yih']");
driver.FindElement(element).Click();
Sign up to request clarification or add additional context in comments.

2 Comments

driver.FindElement(By.XPath("//span[@class='_8scx2 coreSpriteHeartOpen']")).Click(); worked! Thanks!
No problem, glad I could help

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.