Cannot navigate using button with same xpath from separate modal windows

Hello All,

I am attempting to test a web based application where users fill our information for multiple fields which all open modal pop-up windows. The issue I’m incurring is that Katalon studio gives the buttons in the pop-ups the same x-path. It correctly submits within the first pop-up and the opens the second pop-up window but tries to click the button from the first pop-up while in the second pop-up window. The only difference I can see in the xpath between these buttons when viewing with the object spy tool is that a random index is added to the end if the
path, as an example:

Button in first window:
(//button[@type=‘button’])[8]
Button in second window:
(//button[@type=‘button’])[26]

How can I specify which button I want to click? Possibly with the index?

Thank you for any help

3 Likes

Are you using web spy katalon tool for object spying?

If yes please use the highlight button by changing the numbers alone [index] and click on highlight button untill your desired button gets highlighted or better use the attributes to create xpath

3 Likes

@bharathi.a I am using the Object spy from Katalon Studio. I would prefer to use the attributes to create the xpath, however I’m not sure how I would differentiate between the two.

2 Likes

These are very weak xpaths. If you can share the HTML structure for the modal dialog(s), and show where the button element is, we can get you a working one.

3 Likes

@Brandon_Hein I agree, they are extremely weak, however this is all that is picked up by the object spy. I think that part of the issue is that the modals are a common asset and one modal cannot be differentiated from another. I am specifically targeting the “Select” button. The structure of each modal is almost identical, the only difference between the two modal windows is the title, in this case “Lookup records”

1 Like

Then perhaps start your pathway from there and move to the Select button, like:

//h1[text()="Lookup records"]/../following-sibling::div[last()]/button[1]

or

//h1[text()="Lookup records"]/ancestor::div/following-sibling::div[last()]/button[1]

Basically, from the text, “Lookup records”, move up to the parent, then down at the same level to the last <div> and then to the first button.

Understood.

Try this:

(//div[contains(@class, 'modal-dialog')])[last()]//button[@aria-label='Select']

In english, this xpath is roughly “the ‘Select’ button within the top-most modal window”

We have similar scenarios where multiple modals appear on top of each other, so by calling last(), you are referencing the top-most one, then referencing the button within that context.

1 Like

I have attempted the second option given, and receive an element not interactable error.

Using the path provided, I receive this error:

=============== ROOT CAUSE =====================
Caused by: com.kms.katalon.core.webui.exception.WebElementNotFoundException: Web element with id: ‘Object Repository/AuthRepButton’ located by ‘(//div[contains(@class, ‘modal-dialog’)])[last()]//button[@aria-label=‘Select’]’ not found

Can you open the “Console” tab in the browser inspector, and enter the following into your console? Make sure the modal in question is open on your screen before doing this:

$x(“(//div[contains(@class, ‘modal-dialog’)])[last()]//button[@aria-label=‘Select’]”)

This is how you can test xpaths without running the full script. Let me know what comes back. This is a valid xpath based on the HTML you shared, so this should confirm. The only other thing is, you might be working within an iframe, in which case we need to take a different approach.

And if you ever get “an element not interactable error”, then put wait or delay statements to check whether you have a timing issue or not, such as:

button = com.Tools.createTestObject('//h1[text()="Lookup records"]/../following-sibling::div[last()]/button[1]')
WebUI.waitForElementVisible(button, 10)
WebUI.verifyElementVisible(button)
WebUI.click(button)

or, for testing only:

WebUI.delay(3)
WebUI.verifyElementVisible(findTestObject(...))
WebUI.click(findTestObject(...))
1 Like

First use

WebUI.switchToWindowIndex(index value)
OR
WebUI.switchToWindowTitle(’ ')

and put code for some delay then close the popup

It returns “Invalid or unexpected token”

That’s likely a copy/paste problem between discourse and console. Please replace all single and double quotes by typing them in with your keyboard:

I wrote a bit more description about “a copy/paste problem”

See

Try the following Xpaths

  1. //button[@title=“Select”]
  2. //button[text()=‘Select’]

or try
//button[@title=“Select” and text()=‘Select’]
//button[@title=“Select” or text()=‘Select’]

1 Like

This ended up contributing to the solution. Ultimately I had to implement the delay and manually set the xpaths to the very basic ones picked up by the Object Spy and have been able to apply the fix on multiple webpages with similar configuration.

Thank you

1 Like