Search code examples
rrselenium

How to tell RSelenium driver to click on one of two links, depending on which is present?


My goal is to get the driver to loop through multiple pages. On those pages, I want the driver to click another link with the most recent date. The problem is that not all the links have the same selector.

This page only has one date, and the selector is: .captures-range-info a

This page has two dates, and the selector for the most recent date is: .captures-range-info a+ a

This is my code for when there's two dates

click_date = rd$findElement(using = 'css', '.captures-range-info a+ a')
click_date$clickElement()

How can I tell the driver to click on whichever element is available? If this cannot be achieved with RSelenium, is there another package that would be capable of making this distinction?


Solution

  • Basically, what you want is to get the last a element that is in .captures-range-info. This can be done with the CSS selector :last-of-type like this:

    rd$findElement(using = 'css', '.captures-range-info a:last-of-type')