Rishi Arora
Rishi Arora

Reputation: 1793

Can I generate the XPath expression of the HTML page through my Java code?

Using Selenium in Java, how can I get an XPath at runtime? For example, can I generate the XPath to an HTML element through my Java code?

Because the environment will change in the future, I don't want to use Firebug or other such tools to solve the problem. In the new environment, the items will be same, but the id or XPath expression will differ. I want my code to work in any environment.

Upvotes: 0

Views: 811

Answers (1)

Guy
Guy

Reputation: 50899

No, you can't. Your Java code is based on locators (id, class, xpath, etc.) that you find in the HTML content, and your code can't do it for itself.

As for your problem, ID's don't change from environment to environment and usually neither are classes. Try to locate elements by those locators and try to avoid xpath. That way, you shouldn't have a problem running your code in any environment.

Upvotes: 1

Related Questions