I ma trying to find element using RSelenium
remDr <- remoteDriver(remoteServerAddr = "192.168.99.100", port = 4445L,
browserName = "chrome")
remDr$open()
url <- "https://sudskapraksa.csp.vsrh.hr/decisionPdf?id=090216ba8084ca52"
remDr$navigate(url)
There is a captcha image (if you don't see it try to execute navigate part 10 times) I tried to select using:
captcha_element <- remDr$findElement(using = "css selector", "img[id='captchaImg']")$clickElement()
captcha_element <- remDr$findElement(using = "id", "captchaImg")$clickElement()
captcha_element <- remDr$findElement(using = "xpath", "//*[#id='captchaIm']")$clickElement()
but it always return an error.
As per your code trials and subsequent comment update to identify the captcha image you can use the following Locator Strategy but of-coarse you can't invoke the click event as none of the element attributes contain any such event :
captcha_element <- remDr$findElement(using = "css selector", "img#captchaImg")
Related
I'm using RSelenium to do some simple Google searches. Setup:
library(tidyverse)
library(RSelenium) # running docker to do this
library(rvest)
library(httr)
remDr <- remoteDriver(port = 4445L, browserName = "chrome")
remDr$open()
remDr$navigate("https://books.google.com/")
books <- remDr$findElement(using = "css", "[name = 'q']")
books$sendKeysToElement(list("NHL teams", key = "enter"))
bookElem <- remDr$findElements(using = "css", "h3.LC20lb")
That's the easy part. Now, there are 10 links on that first page, and I want to click on every link, back out, and then clink the next link. What's the most efficient way to do that? I've tried the following:
bookElem$clickElement()
Returns Error: attempt to apply non-function - I expected this to click on the first link, but no good. (This works if I take the s off of findElements() - the above, not the for loop below).
clack <- lapply(bookElem, function(y) {
y$clickElement()
y$goBack()
})
Begets an error, kind of like this question:
Error: Summary: StaleElementReference
Detail: An element command failed because the referenced element is no longer attached to the DOM.
Further Details: run errorDetails method
Would it be easier to use rvest, within RSelenium?
I think you could consider grabbing the links and looping through them without going back to the main page.
In order to achieve that, you will have to grab the link elements ("a tag").
bookElems <- remDr$findElements(using = "xpath",
"//h3[#class = 'LC20lb']//parent::a")
And then extracting the "href" attribute and navigate to that:
links <- sapply(bookElems, function(bookElem){
bookElem$getElementAttribute("href")
})
for(link in links){
remDr$navigate(link)
# DO SOMETHING
}
Full code would read:
remDr$open()
remDr$navigate("https://books.google.com/")
books <- remDr$findElement(using = "css", "[name = 'q']")
books$sendKeysToElement(list("NHL teams", key = "enter"))
bookElems <- remDr$findElements(using = "xpath",
"//h3[#class = 'LC20lb']//parent::a")
links <- sapply(bookElems, function(bookElem){
bookElem$getElementAttribute("href")
})
for(link in links){
remDr$navigate(link)
# DO SOMETHING
}
I have following Rselenum code:
library(RSelenium)
remDr <- remoteDriver(remoteServerAddr = "192.168.99.100", port = 4445L, browserName = "chrome")
remDr$open()
# Simulate browser session and fill out form
remDr$navigate("https://oss.uredjenazemlja.hr/public/lrServices.jsp?action=publicLdbExtract")
remDr$findElement(using = "xpath",
"//*[#id='x-auto-29']/input")$sendKeysToElement(list("Beli Manastir", key = "enter"))
Sys.sleep(1L)
remDr$findElement(using = "xpath",
"//*[#id='x-auto-30']/input")$clickElement()
Sys.sleep(1L)
remDr$findElement(using = "xpath",
"//*[#id='x-auto-30']/input")$sendKeysToElement(list("Baranjsko", key = "enter"))
Sys.sleep(1L)
remDr$findElement(using = "id",
"x-auto-32")$sendKeysToElement(list("1"))
Sys.sleep(1L)
remDr$findElement(using = "xpath",
"//*[#id='x-auto-33']/input")$clickElement()
Sys.sleep(1L)
remDr$findElement(using = "xpath",
"//*[#id='x-auto-35']/div[2]")$clickElement()
Sys.sleep(1L)
remDr$findElement(using = "xpath",
"//*[#id='x-auto-14']/tbody/tr/td[2]/em/button")$clickElement()
As you can see new "window" pop up. If I try to select any of the elements in new window but it returns and error. How could I download captcha picture or wright something in text box? I tried windows handlers, but it's not a new window.
EDIT:
I have found on several places that I should use javascript to click hidden elements. I tryed:
script <- "return document.getElementById('x-auto-135').hidden;"
remDr$executeScript(script, args = list())
but get an error:
Error: Summary: UnknownError
Detail: An unknown server-side error occurred while processing the command.
class: java.lang.NullPointerException
Further Details: run errorDetails method
I have finally find the solution:
remDr$findElement(
using = "css selector",
".x-window input[class=' x-form-field x-form-text ']")$sendKeysToElement(list("ded24", key = "enter"))
I'm trying to pull data from glassdoor website using Rselenium. I'm unable to enter email id and password in the popup window.
This is my code. I'm not sure where I'm going wrong. When I try to highlight email box, sign in button is being highlighted.
remDr$navigate("https://www.glassdoor.com")
webElem <- remDr$findElement("class", "sign-in")
webElem$highlightElement()
webElem$clickElement()
email <- webElem$findElement(using = "name", "username")
email$highlightElement()
email$sendKeysToElement(list("EMAIL ID")) -->Throwing Error
The following works with latest chrome:
library(RSelenium)
rD <- rsDriver()
remDr <- rD$client
remDr$navigate("https://www.glassdoor.com")
webElem <- remDr$findElement("class", "sign-in")
webElem$highlightElement()
webElem$clickElement()
remDr$setImplicitWaitTimeout()
email <- remDr$findElement(using = "id", "signInUsername")
email$sendKeysToElement(list("EMAIL ID"))
....
rm(rD)
gc()
I am using R, version 3.3.2. Using Rselenium package, I am trying to scrap some data from this website: http://www.dziv.hr/en/e-services/on-line-database-search/patents/
I am using Rselenium and my code looks like this:
selServ <- RSelenium::startServer(javaargs = c("-Dwebdriver.gecko.driver=\"C:/Users/Mislav/Documents/geckodriver.exe\""))
remDr <- remoteDriver(extraCapabilities = list(marionette = TRUE))
remDr$open()
Sys.sleep(2)
# Simulate browser session and fill out form
remDr$navigate("http://www.dziv.hr/hr/e-usluge/pretrazivanje-baza-podataka/patent/")
This doesn't work:
webel <- remDr$findElement(using = "xpath", "/input[#id = 'TB1']")
Then I wanted to swith to iframe using switchToFrame() function, but the iframe does not contain id.
Then I have tr to use index: webel <- remDr$switchToFrame(1) but this just return NULL
Also, I recognized, iframe has different domain.
Is it possible to svrap data from this web site?
You can just select the first iframe and pass it to the switchToFrame method:
webElem <- remDr$findElements("css", "iframe")
remDr$switchToFrame(webElem[[1]])
webel <- remDr$findElement(using = "xpath", "//input[#id = 'TB1']")
I try to click to go to the next page of search results of google using the following code:
library("RSelenium")
startServer()
checkForServer()
remDr <- remoteDriver()
remDr$open()
remDr$navigate("https://www.google.com/")
webElem <- remDr$findElement(using = "xpath", "//*/input[#id = 'lst-ib']")
webElem$sendKeysToElement(list("R Cran", "\uE007"))
webElem <- remDr$findElement(using = 'css selector', "#pnnext")
click <- webElem$getElementAttribute("href")
remDr$clickElement(click)
However I receive the following error:
Error in envRefInferField(x, what, getClass(class(x)), selfEnv) :
‘clickElement’ is not a valid field or method name for reference class “remoteDriver”
Does click next button to google search results has different code?
Using inspect I can see that the source code for the button is:
<a id="pnnext" class="pn" style="text-align:left" href="/search?q=R+Cran&biw=1366&bih=657&ei=szhxVv_NMaHMygPW4pLQDg&start=10&sa=N">
Finally what was worked for me:
library("RSelenium")
startServer()
checkForServer()
remDr <- remoteDriver()
remDr$open()
remDr$navigate("https://www.google.com/")
webElem <- remDr$findElement(using = "xpath", "//*/input[#id = 'lst-ib']")
Sys.sleep(5)
webElem$sendKeysToElement(list("R Cran", "\uE007"))
Sys.sleep(5)
link <- remDr$executeScript("return document.getElementById('pnnext').href;")
remDr$navigate(link[[1]])
You are trying to "click" an attribute/string, which is not working the way you try it.
On this line you are grabbing a link as a string (which is not a WebElement for Selenium!)
click <- webElem$getElementAttribute("href")
and then in the next line you are trying to click this link/string via a method that actually needs a WebElement
remDr$clickElement(click)
So here is what you can try:
1) you could try to click your last WebElement directly (not doing the getAttribute):
webElem$clickElement()
or
2) you could try to navigate to the link you just got through getAttribute:
click <- webElem$getElementAttribute("href")
// change your last line to this
remDr$navigate(click)
Not sure what client are you using but it might be that you need to wait() until the ajax request finishes. visibilityOfElementLocated #pnext