Home > Net >  My RSelenium code is not able to find web elements within frame
My RSelenium code is not able to find web elements within frame

Time:10-29

I am trying to write code that scrapes web content within a frame, also known as an iframe. I am using RSelenium for that goal. Here is the code I am using:

library(wdman)
library(RSelenium)
library(xml2)
library(selectr)
library(httr)
library(jsonlite)
#Code
# using RSelenium to start firefox on the selenium server
remDr <- rsDriver(
  port = 4445L,
  browser = "firefox"
)
remDr <- remoteDriver(port = 4445L,browser = "firefox")
# open a new Tab on Chrome
remDr$open()
#navigate to your page
remDr$navigate("http://www.emelnorte.com/docs8/html/factura3.html")
Sys.sleep(2)
webElem <- remDr$findElement(using = "id", "vTIPODOCUMENTO")
webElem$clickElement()
Sys.sleep(1)
webElem2 <- remDr$findElement(using = "xpath", "/html/body/form/table/tbody/tr[1]/td/table/tbody/tr[1]/td[1]/select/option[3]")
webElem2$clickElement()
Sys.sleep(1)
webElem3 <- remDr$findElement(using = "id", "vNRODATO")
webElem4 <- remDr$findElement(using = "id", "BUTTON1")
webElem3$clickElement()

The connection works fine but the issue rises when I try to get webElem, webElem2, webElem3 and webElem4. I am getting this message (I will show only for webElem as the rest are equal):

Selenium message:Unable to locate element: #vTIPODOCUMENTO
For documentation on this error, please visit: https://www.seleniumhq.org/exceptions/no_such_element.html
Build info: version: '4.0.0-alpha-2', revision: 'f148142cf8', time: '2019-07-01T21:30:10'
System info: host: 'DESKTOP-4BLI75I', ip: '172.20.10.4', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_301'
Driver info: driver.version: unknown

Error:   Summary: NoSuchElement
     Detail: An element could not be located on the page using the given search parameters.
     class: org.openqa.selenium.NoSuchElementException
     Further Details: run errorDetails method

But when I explore the source of page:

enter image description here

I noticed I am setting the right id. So, anybody can please help if there is any way to get the web elements I need from this page or what I need to do to reach that goal.

Many thanks!

CodePudding user response:

The information you are trying to access is located inside of an iframe tag, which means you have to first switch to that frame before using these otherwise correct CSS and XPath selectors. Insert these two lines of code after the remDr$navigate statement:

myframe <- remDr$findElement("xpath", "/html/body/div/div/div/div/table/tbody/tr[2]/td/iframe")
remDr$switchToFrame(myframe)
  • Related