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?
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")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With