Selenium: Is it possible to concatenate an xpath with a variable? Selenium: Is it possible to concatenate an xpath with a variable? selenium selenium

Selenium: Is it possible to concatenate an xpath with a variable?


You really should anchor your selenium espressions in ID's of elements instead. In 2 months, no-one is going to have the faintest idea of what the xpath points to, especially if the test is broken at the time you need to find out. We routinely add id's to element just to make testing easier. We've learned to accept that we write java code in a special way to make it testable, and I think it's ok for HTML too.


You could just write:

store | //div[@id='main_content']/div[2]/div[2]/div[2]/div[4]/table/tbody/tr[2]/td/form | myFormassertElementPresent | xpath=${myForm}/fieldset/p[1]/input

As Krosenvold said, however, long xpaths can be pretty fragile. If your concerned about readability in your code, and want all your locators in one file where you can make cascading changes, you might want to consider using an UI-Element map.