0
votes

I am writing a dynamic web scraper for a private website, where I am scraping the results of different bar charts available for sports teams. The problem is that the teams city name is the text available in the span element with class 'bar-chart__value', but there are duplicates (i.e. New York, Los Angeles).

It seems the only place where unique values are available for the team name is in the svg element, but I can't figure out how to find the svg element using xpath.

leaderboard <- map_dfr(1:length(a), function(x){
    team <- remDr$findElements("xpath", "//span[@class = 'bar-chart__logo']/*[name() = 'svg']")[[x]]$getElementText()
    if(team == "Average") {
      number <- remDr$findElements("xpath", "//span[@class = 'bar-chart__value']")[[x]]$getElementText()
      avg <<- x
    } else if(x > avg){
      number <- remDr$findElements("xpath", "//span[@class = 'bar-chart__value']
                                 //span[@class = 'play-link__number-span']")[[x-1]]$getElementText()
    } else {
      number <- remDr$findElements("xpath", "//span[@class = 'bar-chart__value']
                                 //span[@class = 'play-link__number-span']")[[x]]$getElementText()
    }
    df <- tibble(unlist(team), unlist(number))
    colnames(df) <- c("Team", specific)
    return(df)
  })

Does anyone know how to use xpath in the findElement method to find an svg element? This is the code returning the error:

remDr$findElements("xpath", "//span[@class = 'bar-chart__logo']/*[name() = 'svg']")[[1]]$getElementText()
1

1 Answers

0
votes

use local-name() instead of name()-> this might include the prefix.

remDr$findElements("xpath", "//span[@class = 'bar-chart__logo']/*[local-name() = 'svg']")[x]$getElementText()