d84_n1nj4
d84_n1nj4

Reputation: 1852

RSelenium: Switching Windows using Window Handle

I've been working with RSelenium all day and still hitting road blocks here and there. My current issue is using the code WebElemReports$clickElement() which clicks a link and a new window opens. I tried to adjust Firefox settings in "about:config" so that it will not open a new window. It doesn't open a window in normal use, but using RSelenium, it opens a new window still. I also looked at this approach but couldn't follow the logic of how it worked:

How to clickElement() and open the link in the same tab

My next thought process was to use the switchToWindow() function along with getWindowHandles(). The code I wrote is as follows:

remDr$closeWindow()
windHand <- remDr$getWindowHandles()
remDr$switchToWindow(windHand)

My thinking is that I will close the current window so that there will only be one handle to reference and pass that handle to the switchToWindow function. I can't find much switchToWindow documentation for R. I receive the following error with using the code above:

Error: Summary: UnknownError Detail: An unknown server-side error occurred while processing the command. class: org.openqa.selenium.WebDriverException

Any help on this would be much appreciated--I tried to research this as much as possible so this won't get marked as a duplicate question like my last post. Many Thanks.

Upvotes: 2

Views: 2600

Answers (1)

Saurabh Gaur
Saurabh Gaur

Reputation: 23805

Actually you can't close main window, you can switch to child window as below :-

# get main window and store to switch back
currWindow <-  remDr$getCurrentWindowHandle()

#gel all windows 
windows <- remDr$getWindowHandles()

#loop through switching child window 
for (window in windows[[1]]) {
  if (window != currWindow[[1]]) 
    remDr$switchToWindow(window)
}

#now do your stuff with child window 

#now close your child window after doing all stuff
remDr$closeWindow()

#now switch back to main window for further stuff 
remDr$switchToWindow(currWindow[[1]])

Upvotes: 5

Related Questions