Paski7
Paski7

Reputation: 321

C# Selenium - How to click on this element?

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();

Upvotes: 2

Views: 10656

Answers (1)

Marcel
Marcel

Reputation: 1463

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();

Upvotes: 3

Related Questions