如何查找<a href=""> element in a webpage using Rselenium?

时间:2015-04-24 18:33:45

标签: r rselenium

I have the following tag in a webpage:

<a target="PARENT" href="/bin-din/WebOb/mom.ko/6/wo/asaksdaksjd
/5.1.5.5.33.23.23">View Data Set</a>

How can I lookup this element in Rselenium? For example if my current session is saved in remDr, what should I search for:

webElem <- remDr$findElement(??)

I need to search the element using it's display link (View Data Set) since the href link changes over time. Thanks much for your help

3 个答案:

答案 0 :(得分:1)

尝试

library(XML)
fileUrl <- ("http:\\wherever you got your file")
doc <- htmlTreeParse(fileUrl, useInternal=T)
xpathSApply(doc, "//a[@href]", xmlGetAttr, "href")

演示:

fileUrl <- "http://kimkardashianonline.org/"
doc <- htmlTreeParse(fileUrl, useInternal=T)
xpathSApply(doc, "//a[@href]", xmlGetAttr, "href")
[1] "http://kimkardashianonline.org/?page_id=2"                                        
[2] "http://www.kimkardashianonline.org/gallery/"                                      
[3] "http://www.kimkardashianonline.org/icons/"                                        
[4] "http://#"                                                                         
[5] "http://kimkardashianonline.org/?page_id=42"   

答案 1 :(得分:0)

在@plafort的回答中,除非您提前知道要将href属性值设置为什么,否则不需要[@href]。所以这是一个普遍的前进方向。这适用于此网址请求。显然'_blank'不是你想要的。


    library(XML)
    library(RCurl)
    gSite <- getURL("http://www.sitepoint.com/web-foundations/target-html-attribute/") 
    sParse <- htmlParse(gSite)
    xpathSApply(sParse, "//a[@target='_blank']", xmlGetAttr, "href") 

答案 2 :(得分:0)

您可以使用:webElem$getElementAttribute("href")