0

Is it possible to get a field's history (if it exists) for a field in an array or something of that sort in selenium? For example, user id field, I can see all IDs that have been used so far.

The purpose I'd like to use this is quickly create new IDs that haven't been used before. For example testID45 is already taken, so I'll use testID46 to create a new one. It's a lazy way to fill out a form without keeping track of the taken IDs.

Faahmed
  • 375
  • 4
  • 21
  • If it's stored somewhere in the DOM, then yes, otherwise no, that's your responsibility. Selenium just looks at the DOM, nothing else. This is logic you'll have to implement yourself. – Arran Aug 19 '15 at 14:49
  • Looks like I'm out of luck then. Browsers probably handle those themselves because of privacy issues. Oh well. I'll keep it open in case someone has a brilliant solution. – Faahmed Aug 19 '15 at 15:00
  • Is it a website you own/have control of? You can still implement a solution. You want to check if the ID has been used before, so either randomly generate one, or use an API in the website to find out if it's in use. – Arran Aug 19 '15 at 15:01

1 Answers1

1

I don't fully understand why you want to create IDs using Selenium. If you would post more info on what problem you are trying to solve, I could try to provide a better answer.

If you want to pull the IDs from existing elements you could do something like this. This finds all INPUT elements that have an ID specified and writes out the IDs. You could parse the IDs and then determine which ID to use next. I wouldn't recommend this because it would be faster to just generate a new ID that will be unique but maybe you need this for some reason.

List<WebElement> ids = driver.findElements(By.cssSelector("input[id]"));
for (WebElement id : ids)
{
    System.out.println(id.getAttribute("id"));
}

I would recommend generating a new ID of your own format that would be unique on the page. This should be good enough for your purposes.

Random rnd = new Random();
String id = Long.toHexString(rnd.nextLong());
System.out.println("testID-" + id); // e.g. testID-cb8e7bac29ec7c7a

There are many other methods of generating strings in this post that you can reference also.

Community
  • 1
  • 1
JeffC
  • 22,180
  • 5
  • 32
  • 55