0

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]
a.click()

but it shows result 'undefined'.

Also when I check events available for this link with

getEventListeners(a)->

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 '18 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 '18 at 7:08
  • Where is your javascript code to perform click function? – Bharat Mane Jul 17 '18 at 8:52
3

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:

driver.findElement(By.linkText("TestLink"));

Code to Perform Click:

WebElement ele_Link= driver.findElement(By.linkText("TestLink"));
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("arguments[0].click();", ele_Link);
| improve this answer | |
  • 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 '18 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. – Bharat Mane Jul 18 '18 at 6:30
0

Try this below code:

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

I hope this will work.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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