Reputation: 3
I need to create a testing automation tool in selenium with c# to test the website of my company. Everything works fine, except one thing: everytime when I reload a task on the website (like "create new user"), the XPath of some fields change. The first name field is as example not "//*[@id=\"0FirstName\"]" anymore, the new xpath would be "//*[@id=\"2FirstName\"]"
and selenium does not recognize that field anymore like before -> program stops working
Is there a way to make a dynamic function to ignore the number before the email?
driver.FindElement(By.XPath("//*[@id=\"0FirstName\"]")).SendKeys("Selenium");
Upvotes: 0
Views: 61
Reputation: 168072
You can use XPath contains() function like:
//*[contains(@id,'FirstName')]
If for some reason it doesn't work, i.e. returns multiple matches you can also implement some form of ends-with() function by using combination of substring() and string-length() functions like:
//*[substring(@id, string-length(@id) - string-length('FirstName')+ 1, string-length(@id))= 'FirstName']
More information: XPath Operators & Functions
Upvotes: 1
Reputation: 13006
you can use contains()
driver.FindElement(By.XPath("//*[contains(@id,'FirstName')]")).SendKeys("Selenium");
Upvotes: 0
Reputation: 1169
You can change your xpath as following:
driver.FindElement(By.Xpath("//*[contains(@id,'FirstName')]"));
Upvotes: 0