Basically in the title. I would like to use the keyboard shortcut for inspect element within the webdriver. I have tried:
remDr <- driver$client
webEl <- remDr$findElement(using = 'xpath',"/html/body/div[1]/div/div/div[2]/button[2]")
webEl$sendKeysToElement(sendKeys = list("\ue008\ue009","i"))
and also with
webEl$sendKeysToElement(sendKeys = list("\ue008\ue009""i"))
But nothing at all happens.
Thanks.
Related
I would like to know how to get a url after using the clickElement function from R's RSelenium package.
Here's an example:
library(RSelenium)
rD <- rsDriver(browser = c("chrome"),
chromever = "98.0.4758.102",
#extraCapabilities = list(chromeOptions = list(args = list("--headless"))),
port = 4580L)
driver <- rD[["client"]]
urll <- "https://www.zapimoveis.com.br/venda/fazendas-sitios-chacaras/ms+campo-grande/?pagina=1"
driver$navigate(urll)
linkimovdescr <- driver$findElement(using = "xpath",
"/html/body/main/section[1]/div[2]/div[3]/section/div/div[1]")
linkimovdescr$clickElement()
Here's the question! How to get the address "https://www.zapimoveis.com.br/imovel/venda-fazenda-sitio-chacara-parque-do-sol-campo-grande-ms-240m2-id-2531139106/"
Note: The linkimovdescr$getCurrentUrl() or driver$getCurrentUrl() command does not answer my question, as it keeps pointing to the home page.
Thanks for any help.
As new tab opens when you click the item, thus we need to switch tabs in order to get url.
1.Clicking the item
linkimovdescr <- driver$findElement(using = "xpath",
"/html/body/main/section[1]/div[2]/div[3]/section/div/div[1]")
linkimovdescr$clickElement()
2.Now get the list of all the tabs by getWindowHandles
df = driver$getWindowHandles()
3.Now switch to the second tab
driver$switchToWindow(df[[2]])
4.Get the url,
driver$getCurrentUrl()
[[1]]
[1] "https://www.zapimoveis.com.br/imovel/venda-fazenda-sitio-chacara-zona-rural-campo-grande-ms-30000m2-id-2552129433/"
I'm trying download data from this GIS site, but some of the button elements I need are in a shadow dom.
My strategy was to run a JS script that uses shadowRoot.querySelector to get the "elementID" and use it to point to the button I need to click. I thought I could use RSelenium::webElement to create the button elements, but I'm getting an error when I try to click on them.
pacman::p_load(RSelenium, glue, dplyr, rvest)
driver <- rsDriver(browser = c("chrome"), chromever = "90.0.4430.24")
chrome <- driver$client
wisc_url <- "https://data.dhsgis.wi.gov/datasets/wi-dhs::covid-19-historical-data-by-county/about"
chrome$navigate(url = wisc_url)
Sys.sleep(5)
# open side panel
wisc_dl_panel_button <- chrome$findElement("css selector", "#main-region > div.content-hero > div.content-hero-footer > div.content-footer.flex-row > div.content-footer-right > div.yielded > button")
wisc_dl_panel_button$clickElement()
# sometimes it needs time to create the file
Sys.sleep(120)
# get elementId from shadow DOM
wisc_dlopts_elt_id <- chrome$executeScript("return document.querySelector('hub-download-card').shadowRoot.querySelector('calcite-card').querySelector('calcite-dropdown')")[[1]]
wisc_dlopts_elt <- webElement(elementId = wisc_dlopts_elt_id,
browserName = "chrome",
port = 4567)
wisc_dlopts_elt$clickElement()
## Error in checkError(res) :
## Undefined error in httr call. httr output: length(url) == 1 is not TRUE
I don't think it's the particular elementID that I'm using. I tried this create-webElement method with other elements that were able to be clicked using the typical findElement >> clickElement method, and I still get the same error.
BTW I can solve this particular problem through a JS script. But I don't know JS that well, so I'd rather have a RSelenium solution that I can more generally apply if different use cases pop-up in the future.
Use find_elements from the shadowr package:
library(shadowr)
remDr <- RSelenium::remoteDriver(
remoteServerAddr = "host.docker.internal",
port = 4445 , browser = "chrome")
remDr$open(silent = TRUE)
wisc_url <- "https://data.dhsgis.wi.gov/datasets/wi-dhs::covid-19-vaccination-data-by-county/explore?location=44.718554%2C-89.846850%2C7.23"
remDr$navigate(url = wisc_url)
Sys.sleep(5)
shadow_rd <- shadow(remDr)
wisc_dl_panel_button <- find_elements(shadow_rd, 'button[aria-describedby*="tooltip"]')
wisc_dl_panel_button[[3]]$clickElement()
wisc_dl_panel_button <- find_elements(shadow_rd, 'calcite-button')
wisc_dl_panel_button[[1]]$clickElement()
For details and examples please check out my github page https://github.com/ricilandolt/shadowr and https://www.shadowr.org/ (Disclosure: I'm a contributor to this package).
Did you try using findElements?
If you get the CSS or Xpath you can try something like that:
frames <- remDr$findElements("css selector", 'your_css')
then
frames[[1]]$clickElement()
That's what I usually do..
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 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