0

I am trying to fill in a login form.

Here is the code to reproduce the error:

library(RSelenium)
require(XML)

RSelenium::startServer()
remDr <<- remoteDriver()
remDr$open()

appURL <- "https://www.schwab.com/public/schwab/nn/login/login.html&lang=en"
remDr$navigate(appURL)

remDr$findElement("id", "LoginId")$sendKeysToElement(list("username"))

This is the error message received: Selenium message:Unable to locate element: #LoginId

I have tried using xpath and css with the same result. I believe the issue has to do with the page using frames, so remDr cannot "see" the login box.

I then run this code:

webElem <- remDr$findElements(value = "//iframe")
sapply(webElem, function(x){x$getElementAttribute('name')})
[[1]]
[1] "loginIframe"

[[2]]
[1] ""


remDr$switchToFrame(1)
remDr$findElement("id", "LoginId")$sendKeysToElement(list("username"))

The same error message is received: Selenium message:Unable to locate element: #LoginId

Any suggestion on how to get the remote browser to locate the login box?

Thank you.

mks212
  • 901
  • 1
  • 18
  • 40

1 Answers1

2

You're right - it's an issue with the frames. The below code worked for me.

iframe <- remDr$findElement("xpath", "*//iframe[@id = 'loginIframe']")
remDr$switchToFrame(iframe)
remDr$findElement("id", "LoginId")$sendKeysToElement(list("username"))
TTR
  • 129
  • 5
  • How did you know that that was the correct syntax for the xpath? The way I get css and xpath is to right click on an element, click inspect and then both firefox and chrome allow me to copy the xpath or css. Since the iframe is not visible in "view source," there is nothing to right click and then copy. Thank you. – mks212 Mar 13 '18 at 18:51
  • 1
    mostly trial and error, but i've used https://www.w3schools.com/xml/xpath_syntax.asp for some basic knowledge. I would suggest you look into the contains() command aswell. It has been really helpful. Lastly, an easy way to check your code is using the $highlightElement() from Rselenium. If the correct element on the site highlights, then you're golden. – TTR Mar 14 '18 at 08:38
  • Thank you TTR for the tips. – mks212 Mar 14 '18 at 13:43
  • 1
    It's very similar, but used for xpath to e.g. selecting nodes with a certain css selector without having to worry about if the node has other selectors aswell. You can read a bit about it [here](http://www.protechskills.com/testing/automation-testing/selenium/usage-contains-starts-functions-xpath) – TTR Mar 14 '18 at 15:43