Using JUnit I'm attempting to automate the eBay login. I cannot select the email/username field because field changes id on refresh. Any ideas?

input size="40" maxlength="64" name="962970435" id="962970435" type="text" autocapitalize="off" autocorrect="off" placeholder="Email or username" class="fld"

4 Answers 4


The following XPath should work:

//input[@placeholder='Email or username'][@type='text']
  • I just verified. This xpath worked.
    – Aalok
    Jun 27, 2017 at 11:35
  • 1
    just added [@type='text'] because were 2 results and worked like a charm. Great thanks. (//input[@type='text'][@placeholder='Email or username'])
    – mdriver
    Jun 27, 2017 at 12:05
  • @mdriver You're version is better. I've updated my answer since this version is more explicit and doesn't need indexing. Jun 27, 2017 at 12:31
  • Xpath is flaky, might break after trivial design change. CSS based locator suggested by @Michael Durrant is much more reliable. Jun 27, 2017 at 14:04
  • @Peter What makes this XPath any more "flaky" than the equivalent CSS locator? Jun 27, 2017 at 15:00

If 'ID' of your webElement changes everytime after refreshing the page in this situation:

First of all look for any other attribute which Is not changing every time In that div node like name, class etc.

Option 1: If this div node has class, name attribute then you can write XPath as below.


Option 2: You can use the starts-with function.

 //input[starts-with(@placeholder,'Email or username')]

Option 3: You can use contains function. Same way you can use contains function as given below.

  • Let me know if it works for you... Jun 27, 2017 at 10:55
  • Thought on options 2 and 3, too, but are not applicable for my example because id number changes randomly. (//input[@type='text'][@placeholder='Email or username']) found out that works. Thanks for your help.
    – mdriver
    Jun 27, 2017 at 12:01

I prefer css selectors:

input[placeholder='Email or username'][type=text]

Also, I'd prefer to scope to the login form, e.g.

form#SignInForm input[placeholder~='[E|e]mail'][type=text]

You may need to scope further with

form#SignInForm div#pri_signin input[placeholder~='[E|e]mail'][type=text]

You can use either CSS Selector or Xpath. As CSS selector is already discussed above, let me point out how i achieved this using Xpath.

To identify , we will have to use a combination of 2 elements separated by AND operator as below.

Xpath : //input[@placeholder='Email or username' AND @type='text']

Hope this helps! Asky

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.