0

I've became lost in the world of xpaths, so got an advice to create custom xpath, instead of coping this one, for example

"/html/body/div[1]/div[4]/div[1]/div[7]/div/div[2]/div[3]/div/form/div[2]/div[3]/div[4]/div/div[1]/div/div/div[2]/div[1]/span[1]/span/span[1]/span/span/span/span/span[2]"

Such xpath makes the test unstable and when the programmer changes some div, test fails. I'm looking for someting like FirePath, doesn't matter for Chrome or Firefox, but couldn't find anything helpful. Is there an extension which will work from me? firepath

the long xpath above is taken with right click and copy xpath of this class: enter image description here

11
  • Why are looking looking for something that is like FirePath but not FirePath? – kirbycope Feb 13 at 17:45
  • why you need custom xpath , please provide link to the website you want and which element you want to find – PDHide Feb 13 at 20:10
  • @PDHide here's the link: amazon.co.uk/gp/cart/view.html?ref_=nav_cart I'm trying to get the xpath of 'Paperback' label, which is type of the book in the basket. Later in my test, I'm asserting that this itewm contains 'paperback' text – locke Feb 14 at 13:12
  • The link points to empty basket Please add link to product and screenshot of element you want – PDHide Feb 14 at 16:43
  • @PDHide, I'm sorry, hope this works: amazon.co.uk/gp/product/133821666X/… I'm adding this book in the basket and want to assert that underlined element contains text 'Paperback.' But his xpath is so long and unreliable ibb.co/d653fYK – locke Feb 14 at 17:34
2

As mentioned in previous answer using custom loctors or tools to find xpath is not a recommended approach. Learn the xpath functions and appraoches.

You can always use parent elemeent to refer a child element uniquely or combine different attributes together.

in your case you can use :

//*[contains(text(),"Paper") and  @class="a-size-small a-color-secondary sc-product-binding" ]

here:

// - means start from root tag and find element any where in the html

* - means any tag

contains - checks for text() node with content "Paper"

and - is used to check for multiple conditions

@class - checks the value for class attribute

so as we are using and , only element with text containing Paper and class as specified will be returned

1
  • thanks, I will try your solution. But first I need to change some element id's before adding the book in the basket, because now my tests fails. It became three weeks working on this test so no one in the real life will give me such a time for 1 minute manual validation – locke Feb 14 at 18:23
1

Automated xpath generators can help sometimes, but will generally be very unstable and you could do better by creating the xpath yourself. Try to find an unique element (maybe one with an 'id' value) as the parent of the element you are trying to reach and then construct your own xpath. Maybe this cheatsheet can help.

1
  • thanks, I've checked your link, but still looks confusing for me. Maybe it's an advanced topic and I couldn't go there yet – locke Feb 14 at 13:18
0

This is a common issue where sometimes creating locators become so confusing and cumbersome that we end up taking help of some automation browser extensions. Working in a professional software testing company, I have used many such extensions which have made the daily automation tasks very smooth. Below are few -

  1. Selenium IDE Recorder - One of the best tools. This will not only provide Xpath but also you can record your entire script based using plug and play feature. This will save lot of your manual effort and you can create an automation script in very less time. Please download chrome extension from this link - https://chrome.google.com/webstore/detail/selenium-ide/mooikfkahbdckldjjndioackbalphokd?hl=en
  2. Katalon Recorder - It also works like Selenium IDE recorder. Download Link - https://chrome.google.com/webstore/detail/katalon-recorder-selenium/ljdobmomdgdljniojadhoplhkpialdid
  3. ChroPath and Firepath are also conventional tools which can help you to attain Xpaths.
  4. You can inspect element and take xpath from your browser itself by following below steps - Step 1) Inspect the required element. Step 2) Right click on the element as mentioned in below screenshot where I tried to capture locator for "Add to Chrome" button. Step 3) Click Copy and then Click "Copy Xpath"

Screenshot - -

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.