Checkout our demo site to practice selenium https://magento.softwaretestingboard.com/

0 like 0 dislike
644 views
by Contributing Tester (54 points)
edited by

I need help in creating XPath for the input type field shown below. In this field all the id's are changing dynamically and there is nothing unique in this field. Kindly help me in identifying the XPath for this field:-

Edit:-----------------

the problem is that there are lots of fields on the screen and each field is under separate div tags with same attributes like this input field.... so when I identify one input field then it will also highlight other fields on the screen, I am using Chrome with F12 option...

3 Answers

1 like 0 dislike
by Expert (572 points)
Have you tried using XPath with regular expressions?

There are many ways you can identify that element apart from using XPath.

Try using identify element using TagName if you have only one input on the page.

You can even try identifying the parent element.

E,g

WebElement parentElementDiv = driver.findElement(By.xpath("Parent element xpath"));

WebElement input = parentElementDiv.findElement(By.tagName("input"));
by Expert (572 points)
If you are using chrome, you can simply right click on the element and copy > copy xpath.

I  will give you XPath with regular expression as well.
0 like 0 dislike
by The go-to Tester (222 points)
edited by
We have a response to your query from Naveen Shekar(https://www.linkedin.com/in/naveen-shekar-11779430/)

At this situation We have lots of options-

Option 1 : Look for any other attribute which Is not changing every time In that div node like name, class etc. So If this div node has class attribute then we can write xpath as bellow.

//div[@class='post-body entry-content']/div[1]/form[1]/input[1]

Option 2 : We can use absolute xpath (full xpath) where you not need to give any attribute names In xpath.

/html/body/div[3]/div[2]/div[2]/div[2]/div[2]/div[2]/div[2]/div/div[4]/div[1]/div/div/div/div[1]/div/div/div/div[1]/div[2]/div[1]/form[1]/input[1]

Option 3 : We can use starts-with function. In this xpath's ID attribute, "post-body-" part remain same every time.

 //div[starts-with(@id,'post-body-')]/div[1]/form[1]/input[1]

Option 4 : We can use contains function. Same way you can use contains function as bellow.

 div[contains(@id,'post-body-')]/div

===========================

From Rahul Upadhyay (https://www.linkedin.com/in/rahul-upadhyay-6562a5141/)

It can be created using "Contains" too with combination of different attributes like text, id, placeholder name, for an instance we can consider an example as

//*[contains(text(),'string')]
by Contributing Tester (54 points)
Hi,
Thanks for the reply. But this I have already used and with this no success... I donot want to use absolute xpath...last opetion I have is regular expressions in xpath as mentioned by Sunil above...But I have to learn that as I have not used that earlier...
Thanks
0 like 0 dislike
by
Try with  POM ...may help you  or try katalon  to record xpath..or try with x or y coordinates, if nothing  happens than request the developer to make the uniqueness in code.


This site is for software testing professionals, where you can ask all your questions and get answers from 1300+ masters of the profession. Click here to submit yours now!

1.4k questions

1.6k answers

866 comments

1.9k users

...