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"]
.
-
I cannot see id="ReportsView" in your HTML source in the question.....That i got from chrome while copying xpath using chrome .vShridhar– Shridhar2019年01月07日 14:39:22 +00:00Commented Jan 7, 2019 at 14:39
-
Try the below xpath. "//a[@onclick='OnReportImageClick()']/img"Kshetra Mohan Prusty– Kshetra Mohan Prusty2019年01月07日 17:42:32 +00:00Commented 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– Shridhar2019年01月08日 05:35:33 +00:00Commented Jan 8, 2019 at 5:35
-
1hey can you explain little more what exactly you trying to doRao– Rao2019年01月08日 06:42:59 +00:00Commented 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– Shridhar2019年01月08日 08:27:01 +00:00Commented Jan 8, 2019 at 8:27
4 Answers 4
Try using the below xpaths,
//a[@class='nav-link nav-toggle']
//span[text()='Reports']
-
Shanila ,The given Xpath is not working .Shridhar– Shridhar2019年01月08日 13:34:53 +00:00Commented Jan 8, 2019 at 13:34
-
@Sridhar ,as Trevor Pye said use wait statement before locating an element.shanila– shanila2019年01月09日 03:30:29 +00:00Commented 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– Shridhar2019年01月10日 08:32:55 +00:00Commented Jan 10, 2019 at 8:32
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.
-
@Shanila and lostIncode ,i have used the given xpaths it doesnt work getting the same error .Shridhar– Shridhar2019年01月08日 12:48:30 +00:00Commented 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– user361222019年01月08日 14:37:42 +00:00Commented Jan 8, 2019 at 14:37
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
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
.