Search code examples
htmlrwebrseleniumfindelement

Cant find the element (xpath) in RSelenium of a specific website


I'm using RSelenium to do some web scraping on the website https://unicancer.sigaps.fr/.

I want to review my team sigaps points (for those who don't know, when you publish an article in a scientific magazine, you get points and the more you have points the more you will get acknowledged). I want to automatize, the collection of those data.

So i already used RSelenium on other website and it worked, but on this specific one i can't find the html tags with those function:

remDr$findElement(using = "xpath", value = "/html/body/div[1]/div/div[2]/form/table/tbody/tr[2]/td[2]/input")$sendKeysToElement(list(username))
remDr$findElement(using = "name", value = "mdp")$sendKeysToElement(list(password))

I tried with the xpath and the name tag but both don"t work. I can't find any of the page elements.

I get this error:

   Selenium message:no such element: Unable to locate element: 
   {"method":"xpath","selector":"/html/body/div[1]/div/div[2]/form/table/tbody/tr[2]/td[2]/input"}
   (Session info: chrome=96.0.4664.45)
   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: 'PD06B6F', ip: '10.208.107.111', os.name: 'Windows 10', os.arch: 'amd64', 
   os.version: '10.0', java.version: '1.8.0_261'
   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

When I'm looking on the page code with my browser I can identify the html elements:

<input name="login" class="text_box" type="text">

So the website has an issue and I need to solve the interaction problem between RSelenium and it.

Here is my whole code (maybe it is a docker problem. I don't really know what it is though I'm not a web developer)

rD <- rsDriver(browser= "chrome", port = 3955L,chromever = "96.0.4664.45")
remDr <- rD[["client"]]
 
remDr$navigate("https://unicancer.sigaps.fr/")
 
remDr$screenshot(TRUE)
 
username <- "xyz"
password <- "lol93"
 
 
remDr$findElement(using = "xpath", value = "/html/body/div[1]/div/div[2]/form/table/tbody/tr[2]/td[2]/input")$sendKeysToElement(list(username))
remDr$findElement(using = "name", value = "mdp")$sendKeysToElement(list(password))
 
rD$server$stop()
remDr$close()
system("taskkill /im java.exe /f", intern=FALSE, ignore.stdout=FALSE)

Solution

  • We have to use switchToFrame which is generally used for iframe. Though there is no iframe for this webiste we use switchToFrame to change focus on frame.

    enter image description here

    #credentials
    username <- "xyz"
    password <- "lol93"
    

    Start the browser

    library(RSelenium)
    driver = rsDriver(browser = c("firefox"))
    remDr <- driver[["client"]]
    remDr$navigate(url)
    url = "https://unicancer.sigaps.fr/"
    

    Now using switchToFrame to focus on frame

    webElem <- remDr$findElements("css", "frame")
    remDr$switchToFrame(webElem[[1]])
     remDr$findElement(using = "xpath", '//*[@id="scrollup2"]/div/font')$getElementText()
    

    Fill in the credentials

    remDr$findElement(using = "xpath", value = '//*[@id="menu_page_enveloppe"]/div[2]/form/table/tbody/tr[2]/td[2]/input')$sendKeysToElement(list(username))
    
    remDr$findElement(using = "xpath", value = '//*[@id="menu_page_enveloppe"]/div[2]/form/table/tbody/tr[3]/td[2]/input')$sendKeysToElement(list(password))
    

    enter image description here