Sam Kohnson
Sam Kohnson

Reputation: 115

Retrieving a element with multiple xpaths

This isn't much of a coding question but one about what options should I take next. I've just started using selenium about a week ago and started to get the hang of most of its function.

I was working on a work project to login into various websites until I ran into a issue where I was getting this error when I tried to find the password field

Exception in thread "main" org.openqa.selenium.ElementNotVisibleException: element not visible

I went ahead and double checked the name & id but I couldn't find the field using locators like .id, name, and xpath. I inspected the xpath with Firebug and noticed that it returned two results instead of one.

Current Xpath looks like this

.//*[@id='password']

I was wondering if anyone could point me to the right direct when dealing with something like this.

Upvotes: 0

Views: 2081

Answers (3)

undetected Selenium
undetected Selenium

Reputation: 193058

Let me try to answer your queries one by one:

  1. error when I tried to find the password field : This is because the property which you have mentioned id/name/css/xpath was incorrect.

  2. ElementNotVisibleException : I doubt it was for the password_field at all. Reason behind that is, normally login field & password field stays on the same container. Either those resides on the HTML DOM (which gets loaded completely once the page loading is complete) or they may reside within a frame/iFrame (there are other measures to handle them which I am not covering here). But those 2 elements stays on the same container. So if you have found out the login_field element, password_field is just a step away.

  3. inspected the xpath with Firebug and noticed that it returned two results : That's because the xpath which you are using doesnot identifies an unique element. There can be multiple elements on the HTML DOM with the same id attribute set to "password". But definitely the xpathof those elements will be different and unique.

  4. What you should do?

    I will suggest you the following:

    a. Try to identify each element on a webpage following the attributes in sequence: id, name, visibleText, css, xpath.

    b. Try to identify a unique logical xpath for elements using the other properties of the elements defined within the HTML tag. Cross check your xpath through Firebug/Firepath. There are some other handy xpath checker available too. Take help of those.

Let me know if this answers your question.

Upvotes: 0

Andersson
Andersson

Reputation: 52665

Normally, id attribute suppose to be unique value, but there are some cases when front-end developer just hide part of DOM, create similar and forget to remove hidden one.

In such case you can (if it possible) ask developer to get rid of redundant code or use index as

"(.//*[@id='password'])[2]"

Upvotes: 1

Guy
Guy

Reputation: 50809

In cases like this you need to use another unique DOM element (parent, ancestor, sibling, child) with connection to the element you are looking for. For example In those html snippets

<div id='a'>
  <div id='password'></div>
</div>

And

<div id='b'>
  <div id='password'></div>
</div>

.//*[@id='password'] will have two results

However

.//*[@id='b']/[@id='password']

Will have only one result

Upvotes: 1

Related Questions