Switch Browser action not works in new tab

Hi,

I’m using the Open Browser action to open a website and perform some operations. When I search some record, it will open a new tab and display the result. I used Switch Browser action to read the detail, but it throws the following exception.

Caused by: org.openqa.selenium.NoSuchElementException: Unable to locate element: //*[@id=“txtTextArea1”]

Please suggest the workaround.

@anandhan Are you trying to use xpath in a new tab? It will not work like this, xpaths only work on the page opened under Open website action. You could use a small custom script like this to use xpaths in different tabs.
Maybe, you could share your recording so we see more details and offer a better workaround?

image

Please find the actions that I tried from the above image

  1. Step 111: set the search text using XPath and hit enter to view the result. it will display the result in the new tab
  2. Step 112: used Switch to Browser action, to get the context of the new tab
  3. Step 114: I could be able to set the value using XPath in the new tab
  4. Step 115: Get value using XPath always returns null/empty

Switch to Browser action doesn’t switch between tabs in the browser, it switches from other open applications to the browser, so you should remove it from the script.

Right now, xpaths don’t work by default on a page that is opened in a new tab - we will work on thi functionality for future releases.

For now, try using such workaround for your use case:
After the result opens in a new tab, use a simple custom action that would switch to the last opened window. After that, you should be able to use xpaths on this page.

The script is similar to the one in the example I provided above:

@CustomScriptAction(
)

def customScript() {
   switchToLastWindow()
}

Here is a sample recording: switch-to-tab-sample.zip (727 Bytes)

Hope it helps.

1 Like

Hi,
I’m trying to apply this method but it seems not to work with version 2.0.1.
In order to be sure I checked selecting an element from the root page after the switchToLastWindow() and it worked, so the tab wasn’t changed.
Browser: Firefox 57

Can you please advise.

did you try it on the recording I posted above?

I also tried that but the element is missing from the page…

This is what i tried
image

We have changed our website since that, so this script broke.

I replaced the url and xpaths, and it worked.

Try changing it:
url - https://www.wikipedia.org/
xpath (action 3) - //*[@id=“www-wikipedia-org”]/div[6]/div[2]/div/div/ul/li[1]/a
xpath (action 5) - //*[@id=“modal-trigger-ember724”]

This also fails. Which element do you click?

It should click Download for iOS link, and then it should open the Version History popup.
Can you post your final recording and the text of the error here?

I confirm that this works, but I used v2.0.4 because the other installation doesn’t have internet access.
So, I’ll check my recording
Thank you for your help.

1 Like

For some unknown reason in my recording it works only when the custom script is the first action in “Switch to Browser” action. So I changed it to the following.

image

1 Like