Unable to locate the element when element has same name

Hi,

This is regarding the case when there are same kind of elements on the same page but under different headers, e.g in below the below screenshot the values for ‘Date from’ and ‘Date to’ are autopopulated and user needs to enter the highlighted value. Using object spy the name of the elements comes to be same and position, ID is different. I dnt want to use ID because of its dynamic nature and Postion does not work.

Can someone suggest how should I proceed.
Thanks!

1 Like

you can use attributes value or you can make unique xpath for the same.

Don’t use the Spy, don’t rely on the Record tool. The tool gets you started, but doesn’t help much. The Test Objects generated by the tools tends to fail (wrong, unfortunate, … ). You have to correct them manually. In order to correct them, you must train yourself for XPath or CSS Selector technology by reading some good tutorials.

E.g,

Unless you learn the basics of XPath and/or CSS Selector, you wouldn’t be able to be productive with Selenium

if you can show html page for both then we can try to help something more.

Hi,

Below are the required tags for both the elements -

Thanks!

First copy value of class from HTML and paste into below section

//*[@class=“Paste value of class here”]

& then copy and past above xpath into below highlighted area of repository

Hi,

But that’s the issue, if you check the screenshots above, class value is same for both the elements so there is not a unique locator.

Thanks!

okay, but you have to try something from your side! Because i have not access of your site.
Can you paste here class value?

Thanks for your inputs. I will try other options.

You can try as //*[@class=“Paste value of class here”][1] for first

//*[@class=“Paste value of class here”][2] for second

Hi @shrutis, As Kaz pointed out learning the basics will help you in the long run.
But for now you can also try…

opsValueLookup1 object would be: (//*[contains(@id, 'ops-valuelookup')])[1]
opsValueLookup2 object would be: (//*[contains(@id, 'ops-valuelookup')])[2]
These would be even more specific.
opsValueLookup1 object would be: (//*[contains(@id, 'ops-valuelookup') and @type = 'text'])[1]
opsValueLookup2 object would be: (//*[contains(@id, 'ops-valuelookup') and @type = 'text'])[2]

Although you say the id attributes are dynamic, you can start your pathway on another element that has some text or stable attribute and move from that element to the one you want. An example could be:

//*[text()="Duty station"]/following/table/tbody/tr[1]/td[1]/input

In order to answer to a question “tell me a XPath expression that correctly point to an HTML element which is … (with some tricky condition)” , we always need to see the whole HTML source of the target web page (line#1 to the end). I would write whole HTML for short of this.

@shrutis

Thank you for sharing screenshots:

However these screenshots don’t help much. The screenshots shows only a small portion of HTML source. The screenshots does not show the whole HTML.

@grylion54 wrote:

Well, I am afraid, @grylion54 was too kind. I am sure, @grylion54 could not see the whole HTML of the @shrutis’ target web page. He made a wild guess how the whole HTML could be, and presented an fake example. But, I guess, @shrutis would not understand what @grylion54 meant deep in his head.

@shrutis

Please, don’t copy and paste the @grylion54’s code into your project. It wouldn’t work.

1 Like

@shrutis,

You can see the whole HTML of your target web page, but others in this forum can’t. You should be aware of this handicap. You may expect that somebody can tell you a correct answer without looking at the whole HTML. No, it is too difficult. You need to share the whole HTML publicly to be answered.

How to share the whole HTML? — You can save the whole HTML as one file using Google Chrome. Once saved, you can attache the file to your forum post.

  1. open your target HTML in Chrome browser. For example, http://example.com
  2. Right-click anywhere of the page in the Chrome window, choose the menu “Save as …”
  3. Choose the type of output: “Web page, Single file”
  4. you will get a .mhtml file on your local disk
    See Wikipedia for “MHTML”.
  5. Please attache the file here in the Katalon user forum if possible.
  6. They can open the MHTML file with Chrome, then they will see a complete replication of the web page.

Are you concerned about security? Does your organization require you not to disclose the whole HTML source to others? ---- In that case, you can’t share the file, of course.

In such case, you can not ask a good question in any public forum. Guys would not be able to help you without information. You have to solve your problem just for yourself.

1 Like

Maybe you can set for dynamic xPath for selected header

to = findTestObject(‘Tessss/Obj_Header’)
if(to == “Duty Station”)
{
//Create dynamic xPath1
strXPath = “//*[contains(@id, ‘Duty station’)]”
to.setSelectorValue(SelectorMethod.XPATH, strXPath)
to.setSelectorMethod(SelectorMethod.XPATH)

}else if(to == “Const Center”)
{
//Create dynamic xPath2
strXPath = “//*[contains(@id, ‘Const Center’)]”
to.setSelectorValue(SelectorMethod.XPATH, strXPath)
to.setSelectorMethod(SelectorMethod.XPATH)
}

2 Likes

Could you please try parent-child relationships. Sometimes , you can navigate to an element by locating its parent element first and then accessing the desired element within it.