How to switch between multiple websites in one script?

Guys would appreciate the help. I have a requirement where I need to get data into a website but get the results from multiple other websites. How do I switch back and forth between different websites. I have tried “window switch” and “open browser” but both dont seem to work? In “open browser” I can not specify the website that needs to be opened.

I also tried “open website” in sequence but that just opens a brand new site causing me to loose all the data that was entered in the previous step.

How much data do you need to get at each site? You could just make a variable for each data point retrieved which will persist past the web command closing or else append to a list variable and then read out into new site.

I cant close the first site as I would loose the earlier entered data. I need to open a second site to do the calculations? Is it not possible to have two websites open in Workfusion and switch between them???

You can - use keyboard commands to open a browser then open several tabs in that browser and use keyboard commands to navigate from one tab to the other as you gather the data you need from each website.

Doesn’t seem like the most elegant way to do it though.

I tried this indeed. Problem is that the XPATH becomes invalid when I switch between the websites.

Can you describe the problem more fully? XPaths don’t change on a website dynamically - they are fixed references on each page to navigate to.

1 Like

Ok I tried the following simple test.

  1. Open website google.com
  2. Type something in search (through xpath)
  3. Click search (through xpath)
    ==> all works fine

Then :

  1. Open website google.com
  2. Type something in search (through xpath)
  3. Click search (through xpath)
  4. open new tab
  5. Type something in search (same xpath as in step 2) => xpath not found

Code:

import com.workfusion.studio.rpa.recorder.api.*
import com.workfusion.studio.rpa.recorder.api.types.*
import com.workfusion.studio.rpa.recorder.api.custom.*
import com.workfusion.studio.rpa.recorder.api.internal.representation.*
enableTypeOnScreen()

timeouts().pageLoadTimeout(10000, java.util.concurrent.TimeUnit.MILLISECONDS)

openChrome(UrlOperations.ensureProtocol("http://www.google.com"))


  $(byXpath("//*[@id=\"lst-ib\"]")).setValue(String.valueOf(board))


    $(byXpath("//*[@id=\"tsf\"]/div[2]/div[3]/center/input[1]")).click()

inDesktop {
sendKeys(StringTransformations.getHotKeyText(116, 2))
}

inDesktop {

        sendKeys(StringTransformations.escapeAutoitText(web.toString()))

}

inDesktop {
sendKeys(StringTransformations.getKeyPressText(28, 13, 10, 0))
}

sleep(5000)

try {
$$(By.tagName(“html”))
} catch (Exception i0) {
throw new IllegalStateException(“Failed switch to browser. It might be closed in preceding actions.”, i0)
}

  $(byXpath("//*[@id=\"lst-ib\"]")).setValue(String.valueOf(board))


    $(byXpath("//*[@id=\"tsf\"]/div[2]/div[3]/center/input[1]")).click()

Are you doing the exact same task on each new tab - in other words is your step actually this:

  1. Open website google.com
  2. type something in search
  3. Open new tab
    4. open website google.com (or are you going to the google search widget on the new tab page)
  4. type something in search

The reason I am asking is that the xpath for the search box in google.ie is //[@id=“gsr”]/script and the xpath to the google widget search box (asuiming you have chrome say) when you open a new tab but not going again to the google website is //[@id=“q”]

They are different.

No matter how many new tabs I open, go to google.com then find the xpath of the search box they are always the same so I suggest to check your steps are the same for subsequent tabs as for the first website open

@Bonnero I appreciate a lot you are trying to help me solve this problem but this is exactly the issue. I had just created the google script as an example. Try it yourself. Take any two web pages. As soon you put them in the second path the xpath (although the same) becomes inaccessible. :disappointed_relieved::sweat:

@peterb
If you have version 1.3.0 installed, you can do it with the help of new custom actions using scripting in groovy.
If you have two tabs, you can use switchToNextWindow() and switchToPrevWindow()to switch between them. If more - switch to window by title, for example. You’ll be able to use xpaths in all tabs.

Here is an example for switchToNextWindow() and switchToPrevWindow() actions.

switch-to-tab.zip (969 Bytes)

2 Likes

@Claudio1 please share your recording