NoSuchElementException 使用 RSelenium 抓取 ESPN
NoSuchElementException scraping ESPN with RSelenium
我正在使用 R(和 RSelenium)从 ESPN 抓取数据。这不是我第一次使用它,但在这种情况下我遇到了一个错误,我无法解决这个问题。
考虑此页面:http://en.espn.co.uk/premiership-2011-12/rugby/match/142562.html
让我们尝试抓取时间轴。如果我检查页面,我会得到 css 选择器
#liveLeft
像往常一样,我和
一起去
checkForServer()
remDr <- remoteDriver()
remDr$open()
matchId <- "142562"
leagueString <- "premiership"
seasonString <- "2011-12"
url <- paste0("http://en.espn.co.uk/",leagueString,"-",seasonString,"/rugby/match/",matchId,".html")
remDr$navigate(url)
页面正确加载。到目前为止,一切都很好。现在,当我尝试使用
获取节点时
div<- remDr$findElement(using = 'css selector','#liveLeft')
我回来了
Error: Summary: NoSuchElement
Detail: An element could not be located on the page using the given search parameters.
我很困惑。我也尝试过使用 Xpath,但没有用。我也尝试过获取页面的不同元素,但没有成功。唯一回馈的选择器是
#scrumContent
来自评论。
该元素位于 iframe
中,因此该元素对 select 不可用。在 chrome
和 document.getElementById('liveLeft')
的控制台中使用 js
时会显示此信息。在整个页面上时,它将 return null
,即元素不存在,即使它清晰可见。要解决这个问题,只需加载 iframe
即可。
如果您检查该页面,您会看到 scr
因为 iframe
是 /premiership-2011-12/rugby/current/match/142562.html?view=scorecard
,来自提供的示例。导航到此页面而不是 'full' 页面将允许元素为 'visible',因此 select 能够 RSelenium
。
checkForServer()
remDr <- remoteDriver()
remDr$open()
matchId <- "142562"
leagueString <- "premiership"
seasonString <- "2011-12"
url <- paste0("http://en.espn.co.uk/",leagueString,"-",seasonString,"/rugby/current/match/",matchId,".html?view=scorecard")
# Amend url to return iframe
remDr$navigate(url)
div<- remDr$findElement(using = 'css selector','#liveLeft')
更新
如果将 iframe
内容加载到变量中然后遍历该变量更适用,则以下示例显示了这一点。
document.getElementById('liveLeft') # Will return null as iframe has seperate DOM
var doc = document.getElementById('win_old').contentDocument # Loads iframe DOM elements in the variable doc
doc.getElementById('liveLeft') # Will now return the desired element.
通常使用 Selenium 时,当您有一个带有 frames/iframes 的网页时,您需要使用 remoteDriver
class:
的 switchToFrame
方法
library(RSelenium)
selServ <- startServer()
remDr <- remoteDriver()
remDr$open()
matchId <- "142562"
leagueString <- "premiership"
seasonString <- "2011-12"
url <- paste0("http://en.espn.co.uk/",leagueString,"-",seasonString,"/rugby/match/",matchId,".html")
remDr$navigate(url)
# check the iframes
iframes <- htmlParse(remDr$getPageSource()[[1]])["//iframe", fun = function(x){xmlGetAttr(x, "id")}]
# iframes[[3]] == "win_old" contains the data switch to this frame
remDr$switchToFrame(iframes[[3]])
# check you can access the element
div<- remDr$findElement(using = 'css selector','#liveLeft')
div$highlightElement()
# get data
ifSource <- htmlParse(remDr$getPageSource()[[1]])
out <- readHTMLTable(ifSource["//div[@id = 'liveLeft']"][[1]], header = TRUE)
我正在使用 R(和 RSelenium)从 ESPN 抓取数据。这不是我第一次使用它,但在这种情况下我遇到了一个错误,我无法解决这个问题。
考虑此页面:http://en.espn.co.uk/premiership-2011-12/rugby/match/142562.html
让我们尝试抓取时间轴。如果我检查页面,我会得到 css 选择器
#liveLeft
像往常一样,我和
一起去checkForServer()
remDr <- remoteDriver()
remDr$open()
matchId <- "142562"
leagueString <- "premiership"
seasonString <- "2011-12"
url <- paste0("http://en.espn.co.uk/",leagueString,"-",seasonString,"/rugby/match/",matchId,".html")
remDr$navigate(url)
页面正确加载。到目前为止,一切都很好。现在,当我尝试使用
获取节点时div<- remDr$findElement(using = 'css selector','#liveLeft')
我回来了
Error: Summary: NoSuchElement
Detail: An element could not be located on the page using the given search parameters.
我很困惑。我也尝试过使用 Xpath,但没有用。我也尝试过获取页面的不同元素,但没有成功。唯一回馈的选择器是
#scrumContent
来自评论。
该元素位于 iframe
中,因此该元素对 select 不可用。在 chrome
和 document.getElementById('liveLeft')
的控制台中使用 js
时会显示此信息。在整个页面上时,它将 return null
,即元素不存在,即使它清晰可见。要解决这个问题,只需加载 iframe
即可。
如果您检查该页面,您会看到 scr
因为 iframe
是 /premiership-2011-12/rugby/current/match/142562.html?view=scorecard
,来自提供的示例。导航到此页面而不是 'full' 页面将允许元素为 'visible',因此 select 能够 RSelenium
。
checkForServer()
remDr <- remoteDriver()
remDr$open()
matchId <- "142562"
leagueString <- "premiership"
seasonString <- "2011-12"
url <- paste0("http://en.espn.co.uk/",leagueString,"-",seasonString,"/rugby/current/match/",matchId,".html?view=scorecard")
# Amend url to return iframe
remDr$navigate(url)
div<- remDr$findElement(using = 'css selector','#liveLeft')
更新
如果将 iframe
内容加载到变量中然后遍历该变量更适用,则以下示例显示了这一点。
document.getElementById('liveLeft') # Will return null as iframe has seperate DOM
var doc = document.getElementById('win_old').contentDocument # Loads iframe DOM elements in the variable doc
doc.getElementById('liveLeft') # Will now return the desired element.
通常使用 Selenium 时,当您有一个带有 frames/iframes 的网页时,您需要使用 remoteDriver
class:
switchToFrame
方法
library(RSelenium)
selServ <- startServer()
remDr <- remoteDriver()
remDr$open()
matchId <- "142562"
leagueString <- "premiership"
seasonString <- "2011-12"
url <- paste0("http://en.espn.co.uk/",leagueString,"-",seasonString,"/rugby/match/",matchId,".html")
remDr$navigate(url)
# check the iframes
iframes <- htmlParse(remDr$getPageSource()[[1]])["//iframe", fun = function(x){xmlGetAttr(x, "id")}]
# iframes[[3]] == "win_old" contains the data switch to this frame
remDr$switchToFrame(iframes[[3]])
# check you can access the element
div<- remDr$findElement(using = 'css selector','#liveLeft')
div$highlightElement()
# get data
ifSource <- htmlParse(remDr$getPageSource()[[1]])
out <- readHTMLTable(ifSource["//div[@id = 'liveLeft']"][[1]], header = TRUE)