Cannot click on HREF Link with Selenium WebDriver C# in NUnit

988 views
Skip to first unread message

John Pulwers

unread,
Mar 11, 2020, 5:01:55 PM3/11/20
to Selenium Users
HTML: <a class="result__a" rel="noopener" href="https://stackify.com/learn-c-sharp-tutorials/">The Best <b>C</b># <b>Tutorials</b> for Beginners to Advanced Programmers</a>

CODE: IWebElement element = driver.FindElement(By.XPath("//a[@href='/stackify.com/learn-c-sharp-tutorials/']")).click(); // CHROME Search

Also: Why won't the BUILD/Compiler allow me to use "click" instead of "Click" which doesn't work either? Is this because Im using NUnit?
Message has been deleted
Message has been deleted

Josh Abrahamsen

unread,
Mar 11, 2020, 11:23:42 PM3/11/20
to Selenium Users
The C# Selenium requires .Click(). It seems to be better suited for C#'s implementation to be uppercase, not sure why the difference though to be honest.

Also, XPATH is okay to use. CSS Selectors are supposed to be faster so try using that but I'll provide both examples for you. Another benefit of CSS Selectors is you can use * for contains where XPATH requires the exact full value, so in your case above using XPATH would require the whole href. XPath is even case sensitive in this case as well.

By.XPath("//a[@href='https://stackify.com/learn-c-sharp-tutorials/']").Click();

Shorter, faster, contains instead of equals, overall better.
By.CssSelector("a[href*='learn-c-sharp-tutorials']").Click();

*Third time's the charm... my apologies*

Adrian

unread,
Mar 18, 2020, 10:42:29 PM3/18/20
to Selenium Users
Hi John,
I would try driver.FindElement(By.LinkText("The Best C# Tutorials for Beginners to Advanced Programmers")).Click();

Cheers,
Adrian.
Reply all
Reply to author
Forward
0 new messages