I have link with below properties on my Webpage:

<li class="XXXX" title="This is test"> TestLink </li>

I am trying with below code for clicking on above link with Javascript:

var a = document.getElementsByClassName("XX")[2]

but it shows result 'undefined'.

Also when I check events available for this link with


it's showing:

blank {}

Please help me to click on this link.

  • You need to provide your HTML here, as well as the class you used
    – Yu Zhang
    Jul 17, 2018 at 7:03
  • I added html code in first para but i think editor converted it to link.Here is the html code : <li class="wux-layouts-treeview-label" title="Shared with me">Shared with me</li>
    – Gitanjali
    Jul 17, 2018 at 7:08
  • Where is your javascript code to perform click function? Jul 17, 2018 at 8:52

2 Answers 2


You can use XPath, linkText or className to locate your Link object.

By XPath:

driver.findElement(By.xpath("//li[@title='This is test']"));

By linkText:


Code to Perform Click:

WebElement ele_Link= driver.findElement(By.linkText("TestLink"));
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("arguments[0].click();", ele_Link);
  • Thank you Bharat for the answer and it will work in selenium. But i am using Visual Studio code to write the javascript code and driver is unknown there(we have to use document instead). I have tried many options with my javascript by even adding the explicit event for click of that link. But no success :(
    – Gitanjali
    Jul 18, 2018 at 5:44
  • Ohh..Please edit your question particular for Visual Studio. You can post the same one on Stackoverflow. Will update you if I got any info on how it works in VS. Jul 18, 2018 at 6:30

Try this below code:

var a = document.getElementsByClassName("XX");

I hope this will work.

  • That was the first code i tried, but unfortunately it didn't work.
    – Gitanjali
    Jul 18, 2018 at 6:27

Your Answer

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

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