RSelenium and Javascript

前端 未结 1 587
灰色年华
灰色年华 2021-01-21 09:57

I\'m fairly proficient in R, but completely ignorant regarding javaScript and other languages. I would like to like to access information on this publicly-available data set (h

相关标签:
1条回答
  • 2021-01-21 10:37

    You dont need to use executeScript here:

    require(RSelenium)
    checkForServer()
    startServer()
    remDr<-remoteDriver()
    remDr$open()
    remDr$getStatus()
    remDr$navigate("http://fyed.elections.on.ca/fyed/en/form_page_en.jsp")
    
    p.codes<-c('m1p4v4', 'n3t2y3', 'n2h3v1')
    webElem<-remDr$findElement(using = 'id', value = "pcode")
    webElem$sendKeysToElement(list(p.codes[1])) # send the first post code to the element
    
    remDr$findElement("id", "en_btn_arrow")$clickElement() # find the submit button and click it
    

    if you wanted to use executeScript instead you would replace the last line with:

    remDr$executeScript("arguments[0].click();"
                    , list(remDr$findElement("id", "en_btn_arrow")))
    

    executeScript takes a script as an argument and a list. If any elements of the list are of class webElement then they can be referred to in the script like a DOM element. In this case the first element (zero index in JavaScript) is a webElement and we ask to click it in our JavaScript.

    Furthermore if you examine the source code behind the button you will find when it is pressed it calls document.pcode.submit() so more simply in this case if you wanted to use executeScript you could do:

    remDr$executeScript("document.pcode.submit();")
    
    0 讨论(0)
提交回复
热议问题