Created
November 16, 2022 18:56
-
-
Save yjunechoe/0b828df2cd2a48ccc35d6c0973013872 to your computer and use it in GitHub Desktop.
RSelenium script to simulate scrolling of a pop-up box to the end, using twitter app reviews on google play store as example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(RSelenium) | |
# Google play store page for twitter app | |
url <- "https://play.google.com/store/apps/details?id=com.twitter.android" | |
# RSelenium firefox browser setup | |
driver <- rsDriver(browser = 'firefox', verbose = FALSE, port = 123L) | |
session <- driver$client | |
session$open() | |
# Navigate to page and click to get review popup | |
session$navigate(url) | |
session$findElement(using = 'xpath', value = '//*[text()="See all reviews"]')$clickElement() | |
# Popup div | |
reviews_popup <- session$findElement(using = "css", value = ".fysCi") | |
get_popup_height <- function() { | |
as.integer(reviews_popup$getElementAttribute("scrollHeight")) | |
} | |
get_reviews_n <- function() { | |
session$executeScript( | |
script = 'return arguments[0].querySelectorAll(".RHo1pe").length', | |
args = list(reviews_popup) | |
) |> as.integer() | |
} | |
# Simulate scrolling to see more reviews, until end or some threshold on number of reviews | |
cur_popup_height <- 0 | |
while(get_popup_height() > cur_popup_height && get_reviews_n() < 500) { | |
cur_popup_height <- get_popup_height() | |
session$executeScript( | |
script = "arguments[0].scrollTo(0, arguments[1])", | |
args = list(reviews_popup, cur_popup_height) | |
) | |
Sys.sleep(.5) | |
cat(cur_popup_height, "\n") | |
} | |
get_reviews_n() | |
#> [1] 520 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
20221116_135154.mp4