0

I am not able click on the specific element, which is in the image format. I have tried using available locators i.e id and image using XPath and remaining locators are same for all remaining menus.

Below is the HTML code. In the below code class locator is same for all other menus. How to automate this type of scenario?

<a href="#" class="nav-link nav-toggle" onclick="OnReportImageClick()">
   <img src="/Images/reports.png">
   <span class="title">Reports</span>
</a>

The XPath I have tried is //*[@id="ReportsView"].

7
  • I cannot see id="ReportsView" in your HTML source in the question.....That i got from chrome while copying xpath using chrome .v
    – Shridhar
    Commented Jan 7, 2019 at 14:39
  • Try the below xpath. "//a[@onclick='OnReportImageClick()']/img" Commented Jan 7, 2019 at 17:42
  • Kshetra mohan,i have tried the given xpath but it doesnt work.please find the below error .Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//a[@onclick='OnReportImageClick()']/img"} (Session info: chrome=71.0.3578.98) (Driver info: chromedriver=2.45.615291 (ec3682e3c9061c10f26ea9e5cdcf3c53f3f74387),platform=Windows NT 10.0.14393 x86_64) (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 0 milliseconds For documentation on this error, please visit:
    – Shridhar
    Commented Jan 8, 2019 at 5:35
  • 1
    hey can you explain little more what exactly you trying to do
    – Rao
    Commented Jan 8, 2019 at 6:42
  • Rao, The exact functionality is as follows.After login to my application,In dashboard if i click on 'reports' it will open new window with its functionality.In that page I need to click on a menu called reports but i'm not able to click on that i have tried with different xpath and all but it doesnt work.
    – Shridhar
    Commented Jan 8, 2019 at 8:27

4 Answers 4

0

Try using the below xpaths,

//a[@class='nav-link nav-toggle']
//span[text()='Reports']
3
  • Shanila ,The given Xpath is not working .
    – Shridhar
    Commented Jan 8, 2019 at 13:34
  • @Sridhar ,as Trevor Pye said use wait statement before locating an element.
    – shanila
    Commented Jan 9, 2019 at 3:30
  • Thanks to everyone ,I have added the window handeld code now it is working fine.Because after switching on to new window i forgot to add window handel code so that is causing an issue.
    – Shridhar
    Commented Jan 10, 2019 at 8:32
0

I think the easiest locator for you would be the actual text of the menu item, that is "Reports". One way to go about it is to locate the <span> element containing said string and then select it's parent.

This XPath should work: //span[text()="Reports"]/..

That will return you the <a> parent element.

2
  • @Shanila and lostIncode ,i have used the given xpaths it doesnt work getting the same error .
    – Shridhar
    Commented Jan 8, 2019 at 12:48
  • @Shridhar that`s weird. As @Trevor Pye says in his answer, are you waiting for the element to be visible? It might be that the DOM is still loading and your element isn't loaded at the moment you are looking for it.
    – user36122
    Commented Jan 8, 2019 at 14:37
0

I have had similar issues where I thought I was selecting an object only to find out it has not loaded yet.

Try to wait for the object before selecting it.

Locating a set of elements by class attribute when class attribute contains multiple values

0

Shridhar, the HTML you provided is an anchor tag. Have you tried locating the element via LinkText or PartialLinkText? Examples below:

driver.findElement(By.linkText("Reports")).click(); 

Or:

driver.findElement(By.partialLinkText("Reports")).click();  

I ask because both of the options above only work with anchor tags.

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.