2

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.

Community
  • 1
  • 1
d84_n1nj4
  • 1,712
  • 6
  • 23
  • 40
  • Can you share the HTML code of this link? Also try to print out the windHand variable and see whats in it? Is there one value or more? Check the section on switchToWindow at the end in this page - https://cran.r-project.org/web/packages/RSelenium/vignettes/RSelenium-basics.html – Grasshopper Aug 11 '16 at 19:42
  • Hi, the output is as follows for windHand, `> windHand <- remDr$getWindowHandles()`, `windHand` `[[1]]`, `[1] "{ccdc1fb0-d9c8-4bc7-a5d0-b69b1027cf8a}"`, I apologize but I can't share the HTML. – d84_n1nj4 Aug 11 '16 at 20:02
  • Does the link tag have a target attribute? If so what is the value in it? – Grasshopper Aug 11 '16 at 20:13
  • I don't believe it does--I'm assuming it would be in the same location as the names, ids, and classes, correct? It has an id, class, name, value, type and style, but I don't see target under Elements. – d84_n1nj4 Aug 11 '16 at 20:18
  • Is this link within any frames or iframes? – Grasshopper Aug 11 '16 at 20:27
  • Also can you try to use the code that is provided in the solution link that you mentioned in your question? What is does is that it finds the link and using javascript makes the target attribute on it as blank. Giving it a blank should open up the next page in the same window – Grasshopper Aug 11 '16 at 20:27
  • The `switchToWindow` method requires a string as input. In this case: `remDr$switchToWindow(windHand[[1]][1])` would pass the first windows handle. The basic vignette has a section on frames and windows `http://rpubs.com/johndharrison/12843` – jdharrison Aug 11 '16 at 23:06
  • Thanks for the help @Grasshopper. I think I needed to be a little more knowledgeable in this field of work and we would have gotten to the same results. – d84_n1nj4 Aug 12 '16 at 17:22
  • Thanks for the suggestion @jdharrison!! I'll keep that trick in mind! Also, nice page in RPubs, I'm sure I'll be visiting it more! – d84_n1nj4 Aug 12 '16 at 17:24

1 Answers1

5

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]])
Saurabh Gaur
  • 23,507
  • 10
  • 54
  • 73
  • 1
    Thank you very much!! At first I thought it was not working until I confirmed I was in the correct window handle. Then I found out the child window, unlike the parent window, had frames. So I had to work through the frames. – d84_n1nj4 Aug 12 '16 at 17:19
  • 4
    It's error in the version I currently use. The line `for (window in windows[[1]]) {` should be `for (window in windows) {` – Peter.k May 19 '17 at 20:04
  • @Peter.k I modified the code to remove the first list element from windows. – StatsStudent Jul 26 '20 at 00:17