Two Objects have same Xpath?

When I capture 02 different objects (two input box), they have same Xpath and are appeared in a same object in Object Repository. So, I can input data in both of them with different data. Help me, plz.

2 Likes

provide numbers to your xpath! For example if both text fields have //input[@id=ā€˜somethingā€™] then you can edit the first field xpath as (//input[@id=ā€˜somethingā€™])[1] and the second fieldā€™s xpath as (//input[@id=ā€˜somethingā€™])[2] in object repository.

1 Like

I donĀ“t understand, I have two elements with same xpath and when I select the second one the program write the text in first one. How can I tell the program I want to write in the second one?

Jacqueline,

Could you take screenshots and paste them here:
1. HTML fragment of your two input boxes
2. Your 1st Test Object (dont forget to include its xpath in the screenshot)
3. Your 2nd Test Object as well

I have the same problem with this captured object by object spy :

Detect object by :

tag equals span (Detect object by = yes)

class equals fa fa-trash fa-lg (Detect object by = yes)

xpath equals id(ā€œui-accordiontab-5-contentā€)/div[@class=ā€œui-accordion-content ui-widget-content ng-tns-c39-9 ng-star-insertedā€]/display-localisation[1]/div[@class=ā€œimg-containerā€]/a[@class=ā€œng-star-insertedā€]/span[@class=ā€œfa fa-trash fa-lgā€] (Detect object by = no)

How can I tell object repository to select the 2nd element found with those properties please ?

1. I suggest finding a better xpath selector.

2. As a (hopefully temporarily) workaround, for the xpath ā€˜//id(ā€œui-accordiontab-5-contentā€)/div[@content ui-widget-content ng-tns-c39-9 ng-star-inserted"]/display-localisation[1]/div[@container"]/a[@inserted"]/span[@class="fa fa-trash fa-lg"]ā€™ you can add (element number - 1), like in arrays:

//id("ui-accordiontab-5-content")/div[@content ui-widget-content ng-tns-c39-9 ng-star-inserted"]/display-localisation[1]/div[@container"]/a[@inserted"]/span[@class="fa fa-trash fa-lg"](1) 

**EDIT **(because I am unable to edit my own answer):

The array element should go inside of [] instead of (), and the whole xpath expression should go inside of ():

(//id("ui-accordiontab-5-content")/div[@content ui-widget-content ng-tns-c39-9 ng-star-inserted"]/display-localisation[1]/div[@container"]/a[@inserted"]/span[@class="fa fa-trash fa-lg"])[1]

Thanks.

I canā€™t work on this test rigth now but I tried your solution on another one.

I tried (//*([@class = ā€˜ng-star-insertedā€™ and @title = ā€˜ERASEā€™][2]) but I get "

not a valid XPath expression"ā€¦

//*[@class = ā€˜ng-star-insertedā€™ and @title = ā€˜ERASEā€™][2]

1 Like

@Normand Bourget

You have an extra ā€œ(ā€. Try Trong Buiā€™s solution or:

(//*[@class = ā€˜ng-star-insertedā€™ and @title = ā€˜ERASEā€™])[2]

1 Like

Thks, I donā€™t have a syntax error anymore.

But even if Katalon can finds 2 elements, it still canā€™t select 2nd element (I simplified my XPATH for this example) :

09-28-2018 04:39:00 PM - [INFO] - Finding web element with id: ā€˜Object
Repository/Socle/socleLocalisationAdresseEraseā€™ located by ā€˜By.xpath:
//*[@title = ā€˜ERASEā€™]ā€™ in ā€˜30ā€™ second(s)

09-28-2018 04:39:00 PM - [INFO] - Found 2 web elements with id: ā€˜Object
Repository/Socle/socleLocalisationAdresseEraseā€™ located by ā€˜By.xpath:
//*[@title = ā€˜ERASEā€™]ā€™ in ā€˜30ā€™ second(s)

09-28-2018 04:39:00 PM - [INFO] - Clicking on object: ā€˜Object
Repository/Socle/socleLocalisationAdresseEraseā€™

09-28-2018 04:39:00 PM - [PASSED] - Object: ā€˜Object
Repository/Socle/socleLocalisationAdresseEraseā€™ is clicked on

09-28-2018 04:39:00 PM - [END] - End action : click

09-28-2018 04:39:00 PM - [START] - Start action : click

09-28-2018 04:39:00 PM - [INFO] - Finding Test Object with id ā€˜Object
Repository/Socle/GSCP/socleLocalisationPointEraseā€™

09-28-2018 04:39:00 PM - [INFO] - Checking object

09-28-2018 04:39:00 PM - [INFO] - Checking timeout

09-28-2018 04:39:00 PM - [INFO] - Finding web element with id: ā€˜Object
Repository/Socle/GSCP/socleLocalisationPointEraseā€™ located by ā€˜By.xpath:
//*[@title = ā€˜ERASEā€™][2]ā€™ in ā€˜30ā€™ second(s)

09-28-2018 04:39:31 PM - [FAILED] - Unable to click on
object ā€˜Object Repository/Socle/GSCP/socleLocalisationPointEraseā€™ (Root cause:
com.kms.katalon.core.webui.exception.WebElementNotFoundException: Web element
with id: ā€˜Object Repository/Socle/GSCP/socleLocalisationPointEraseā€™ located by
ā€˜By.xpath: //*[@title = ā€˜ERASEā€™][2]ā€™ not found)

Can you replace * in
//*[@title = 'ERASE'][2]
to concrete element name such as
//div[@title = 'ERASE'][2]
?

kazurayam said:

Can you replace * in
//*[@title = 'ERASE'][2]
to concrete element name such as
//div[@title = 'ERASE'][2]
?

Still canā€™t find 2nd elementā€¦

Strange. I need to reproduce your problem and work on it. Could you provided ā€¦

(0) version of your Katalon Studio

(1) full HTML source code of the target HTML, attach the text file here

(2) screen shot of the test object ā€˜Object Repository/Socle/socleLocalisationAdresseEraseā€™

(3) screen shot of the test object ā€˜Object Repository/Socle/GSCP/socleLocalisationPointEraseā€™

Sorry but Iā€™m not legally allowed to provide HTML source out
of officeā€¦

Katalon version is 5.6

How can I provid screen shots here ?

Anyway, below the 2 objects :

SocleLocalisationAdresseErase is xpath equals //*[@title =
ā€˜ERASEā€™]

SocleLocalisationPointErase is either xpath equals
//*[@title = ā€˜ERASEā€™][2] or xpath equals //div[@title = ā€˜ERASEā€™][2] (I tried both
but same result : not found)

Thanks a lot for your help.

Did you try searching for //div[@title = ā€˜ERASEā€™][2] in DevTools?

Enter this to the console and see if the element is found:

$x("//div[@title = 'ERASE'][2]")

DevTools???!?

(In Chrome)
Open browser on the page you are testing. Press F12 -> DevTools will show up.
Go to the ā€œConsoleā€ tab and enter

$x("//div[@title = 'ERASE'][2]")

If the xpath is correct, and the element is present on page, the div with title ā€œeraseā€ should be highlited.

The results (and more tests) :

$x("//div[@title = ā€˜ERASEā€™]")

[]

$x("//div[@title = ā€˜ERASEā€™][2]")

[]

$x("//*[@title = ā€˜ERASEā€™]")

(2) [a.ng-star-inserted, a.ng-star-inserted]

$x("//*[@title = ā€˜ERASEā€™][2]")

[]

$x("//[@title = ā€˜ERASEā€™][1]")
(2) [a.ng-star-inserted, a.ng-star-inserted]
$x("//
[@title = ā€˜ERASEā€™][last()]")
(2) [a.ng-star-inserted, a.ng-star-inserted]
$x("//[@title = ā€˜ERASEā€™][position()=1]")
(2) [a.ng-star-inserted, a.ng-star-inserted]
$x("//
[@title = ā€˜ERASEā€™][position()=2]")
[]

So the question remains : why Katalon can find 2 elements
but canā€™t find the 2nd one separately ??

09-28-2018 04:39:00 PM - [INFO] - Finding web element with id: ā€˜Object
Repository/Socle/socleLocalisationAdresseEraseā€™ located by ā€˜By.xpath:
//[@title = ā€˜ERASEā€™]ā€™ in ā€˜30ā€™ second(s)09-28-2018 04:39:00 PM - [INFO] - Found 2 web elements with id: ā€˜Object
Repository/Socle/socleLocalisationAdresseEraseā€™ located by 'By.xpath:
//
[@title = ā€˜ERASEā€™]ā€™ in ā€˜30ā€™ second(s)

Katalon can select only the second. This is how it worked for me:

(//*[@class = 'ng-star-inserted' and @title = 'ERASE'])[last()]

Note: this is not the DevTools but I wrote this as a xpath selector in the Object Repository. The braces () are around the xpath and the [last()] part is added later. Try this, please.

2 Likes