0

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");

3 Answers 3

1
  1. You can use XPath contains() function like:

    //*[contains(@id,'FirstName')]
    
  2. 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

Sign up to request clarification or add additional context in comments.

Comments

0

You can change your xpath as following:

driver.FindElement(By.Xpath("//*[contains(@id,'FirstName')]"));

Comments

0

you can use contains()

driver.FindElement(By.XPath("//*[contains(@id,'FirstName')]")).SendKeys("Selenium");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.