Terence Xie
Terence Xie

Reputation: 185

How to know the webpage is using dynamic id

For test automation, how can we know if the webpage is using a dynamic id. Here, I don't mean how can we get the dynamic id. Instead, I want to know how to use API, e.g. Selenium API, to know if the web element is using dynamic id.

For example, some ComboBox's id will not change when we select its option. But for other ComboBox's id, it may change as selecting its option.

So, do we have a way to detect, or to know, if the web element in the webpage is using a dynamic id?

Upvotes: 0

Views: 681

Answers (1)

acdcjunior
acdcjunior

Reputation: 135762

No, unfortunately you have no way to discover if and id will change using Selenium.

The only way an HTML element could have its id attribute changed is through JavaScript code. And the only way to know if a piece of JavaScript code changes an id is to execute it. (In other words, you can't, ahead of time, know the id will change. You can only know that JS code changes an id when it actually changes the id, and by this time, of course, the changing already happened.)

If, on the other hand, you want to know if an element had its id changed, you can, before the change takes place, get a hold of it through Selenium (using driver.findElement()) and then keep checking if its attribute changed. (If the WebElement instance becomes stale in the process, you could, as a workaround, add a class with a unique name to it, and then find it back using by.className() - since you may not find it again by its id like before, as it could have changed.)

Upvotes: 1

Related Questions