Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RSelenium: Find link with Xpath

I want to find all links to PDF files in a page with RSelenium and Xpath.

Please consider

require(RSelenium)
RSelenium::checkForServer()

RSelenium::startServer()

remDr <- remoteDriver()
remDr$open()

remDr$navigate("https://cran.r-project.org/manuals.html")

In the page there are multiple links to PDF files such as

<a href="doc/manuals/r-release/R-intro.pdf">PDF</a>

But my first try

remDr$findElement(using = "xpath", "//a[contains(@href,'.pdf')/@href")

produces the following error

Error:   Summary: InvalidSelector
     Detail: Argument was an invalid selector (e.g. XPath/CSS).
     class: org.openqa.selenium.InvalidSelectorException

Am I getting the syntax wrong?

like image 891
CptNemo Avatar asked Oct 28 '15 17:10

CptNemo


1 Answers

There is a syntax error inside your expression, missing a closing ]:

//a[contains(@href,'.pdf')]/@href
                      HERE^

But, even if you fix it, you'll get an error - a different one this time. This is because XPath expressions in selenium have to point to web elements and not element attributes. In other words, use //a[contains(@href,'.pdf')] to find an element and then get_attribute method to get the href attribute value.


Note that you may also find the link by link text:

remDr$findElement(using = "link text", "PDF")
like image 200
alecxe Avatar answered Oct 03 '22 02:10

alecxe