1

I am using Selenium WebDriver along with Java. I am trying to access the 'Login ID' text box in that page but every time I am Logging in and out, the XPath value against the 'Login ID' text box changes so I am unable to detect the 'Login ID' text box every time with the same code.

What modifications do I need to make in my code so that I am able to access all the dynamic XPaths with a single piece of code?

halfer
  • 19,824
  • 17
  • 99
  • 186
S. Mukherjee
  • 77
  • 1
  • 2
  • 6
  • Ask the dev to assign it an unique id. Then you can find it whatever the dom is. – Grasshopper Nov 24 '17 at 11:23
  • Use dynamic locators in the form of `xpath` or `cssSelector` – undetected Selenium Nov 24 '17 at 11:44
  • Please read [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](//meta.stackoverflow.com/q/326569) - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. – halfer Nov 24 '17 at 12:28
  • Could you please provide a sample of the html, you could use contains xpath – Danny Nov 24 '17 at 12:54

1 Answers1

3

Below is an example of what you could potentially use as a workaround

Type of xpaths:

  • multiple matches: //div[@class='class' and contains(text(), 'text')]
  • partial match: //span[contains(class, 'class')]
  • starts-with: //input[starts-with(@name,'input')

These are more beneficial when handling dynamic elements and will be robust.

For more information please see: https://sqa.stackexchange.com/questions/10342/how-to-find-element-using-contains-in-xpath

Danny
  • 287
  • 2
  • 15