How to select an element whose id/number is dynamic and keeps changing



  • I have a number that comes to my email that I would like to copy and save but the number keeps changing all the time.

    dynamic number

    If I take the xpath it will contain that number (//b[contains(.,'821016')]) and I can't use anything from the source (at least I think I can't):

    enter image description here

    Can I use the location to copy the random numbers with CSS or maybe some kind of 0-9/* functions or any other suggestions?

    Update: TushaGT:

    //table/td/span/b - it picks up 2 elements: enter image description here

    And I need only the dynamic number, I got it to copy both through getText but I can't paste. I get the following error when I try:

    Exception in thread "main" java.lang.IllegalArgumentException: Keys to send should be a not null CharSequence
    


  • You can use

    //table/td/span/b
    

    as TushaGT suggests

    However this will easily lead to problems if:

    • you have more than one table
    • you have more than one td (almost certain)
    • you have td's with more than 1 span

    Usually one looks for id's or classes or surrounding nodes that can be used, however there is none in your case.

    In fact the HTML you are showing is so 'bare' I actually think you have two options in this situation:

    • get the developer to add the tags
    • use the full xpath (browser can show that, e.g. right-click) starting at body and accept that any page layout change is going to breask your specs and they will need to be fixed. However until the page does change it will work.

Log in to reply
 

Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2