0

I am not able to find the element in the selenium webdiver with java the Details are given below. I had used all class as given below in addition to Xpath:

<a href="#">
    <span>
        <i class="fa fa-users"></i>
        Employee
    </span>
    <span class="left-menu-list-right-icon">
        <i class="fa fa-chevron-right" aria-hidden="true"></i>
    </span>
</a>

Xpath is given as below

.//*[@id='sidebar-menu']/ul/li[2]/a

The element is not found

5
  • please add the html code from the page/element you're trying to find and the code you're using.
    – Swagin9
    Commented Mar 17, 2017 at 16:15
  • What is the error you are getting? What is the Xpath you are using? Which element do you want to get? This HTML contains 5 elements. If the first <a> is the element we need more HTML. Commented Mar 17, 2017 at 16:21
  • Xpath is given as below .//*[@id='sidebar-menu']/ul/li[2]/a element not found issue
    – Chetan Ma
    Commented Mar 17, 2017 at 16:27
  • You need more HTML to show that the xpath actually matches to the link you're trying to find. It would also help to clarify what "not able to find the element" means, and what selector you've tried to use.
    – ernie
    Commented Mar 17, 2017 at 16:35
  • 3
    Possible duplicate of How to generate good locators for elements in Web pages?
    – dzieciou
    Commented Jul 15, 2017 at 19:37

1 Answer 1

1

Assuming there is no easier way and nothing unique about the elements here, the way I would do this is to first retrieve all elements containing at least one child with class selector "fa-users" (note: I am using pseudo-code - the calls may not be exactly correct):

elements = browser.findElements(By.Class, "fa-users").Parent();

Next, I would loop through the list of parent elements to find the one with inner text that contains the word "Employee", something like:

if element.InnerText().Contains("Employee") then element.Click();

The reason I would use this method is that it looks like this application uses dynamically generated DOM elements, which means that the XPath you found as a selector may not be the same as the XPath to the element when you run your test.

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