2

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!

user007
  • 347
  • 1
  • 3
  • 12

1 Answers1

1

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)
dcsuka
  • 2,922
  • 3
  • 6
  • 27
  • Great! I will check and I will accept it! – user007 Oct 26 '22 at 23:08
  • Hi. I am having some issues with Rselenium, I do not know what is happening. I have posted as question, maybe could you give a check? https://stackoverflow.com/questions/74708282/rselenium-is-not-working-when-creating-servers – user007 Dec 07 '22 at 23:11
  • @user007 Hello, I am not sure about this one sadly. It's probably a bug in the code. I see somebody else on GitHub has the same issue... – dcsuka Dec 08 '22 at 02:31
  • oh many thanks sir, do you believe if I start a bounty somebody could help? – user007 Dec 08 '22 at 15:03
  • Hi sir, sorry for wasting your time, months ago the solution you helped worked fantastic, today I tested again and is not returning any data. If you have any time, could you please check this question? Many thanks! https://stackoverflow.com/questions/74735642/issues-retrieving-links-from-google-search-using-rvest – user007 Dec 08 '22 at 22:39
  • 1
    @user007 I am unsure how useful a bounty would be as the question is very niche and likely to do with the package development. And I am very happy to help you, no time helping others is ever wasted. – dcsuka Dec 09 '22 at 02:40
  • Hi dear I hpe you are fine. I just wanted to thank you for all help this year and I hope you have a marvelous 2023! I am here with a question related to selenium, If you have some time checking this it would be fantastic many thanks! https://stackoverflow.com/questions/74953813/using-rselenium-to-download-a-file-embedded-into-a-frame – user007 Dec 29 '22 at 16:46