2

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"
1

The following XPath should work:

//input[@placeholder='Email or username'][@type='text']
  • I just verified. This xpath worked. – Aalok Jun 27 '17 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 '17 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. – NotInventedHere Jun 27 '17 at 12:31
  • Xpath is flaky, might break after trivial design change. CSS based locator suggested by @Michael Durrant is much more reliable. – Peter M. Jun 27 '17 at 14:04
  • @Peter What makes this XPath any more "flaky" than the equivalent CSS locator? – NotInventedHere Jun 27 '17 at 15:00
3

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.

//input[@class='fld']
OR
//input[@type='text'][@class='fld']
OR
//input[@name='962970435']

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.

 //input[contains(@class,'fld')]
 //input[contains(@type,'text')]
  • Let me know if it works for you... – Bharat Mane Jun 27 '17 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 '17 at 12:01
1

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]
0

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, privacy policy and cookie policy

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