Search code examples
rxpathshinyrseleniumweb-testing

Finding element location in Shiny


I have a Shiny app with varying table sizes depending on inputs and I am trying to test the app using RSelenium. I would like to find the element location using XPath syntax. Finding one element using exact node works fine, however, finding several ones does not return any results at all. My Shiny app cant be shared but the same results occur on a Shiny hosted app by RStudio.

library(RSelenium)

rd <- rsDriver()
r <- rd$client
r$navigate('https://shiny.rstudio.com/gallery/datatables-demo.html')
r$switchToFrame(r$findElements("css selector", "iframe")[[1]])


e <- r$findElements('xpath', "//*[@id='DataTables_Table_0']/tbody/tr[1]/td[3]")
e[[1]]$getElementText()
e[[1]]$getElementLocation()[c('x', 'y')]
# Works as expected 


# Find all elements - does not find any elements

e_all <- r$findElements('xpath', "//*[@id='DataTables_Table_0']/tbody/tr[*]/td[*]")

Solution

  • In the first XPath you are selecting the third td element that is a child of the first tr element under tbody.

    In the second XPath you are selecting the td element (only if it has a child element) that is a child of a tr element that has child elements(which it has to, since you want to select the td child element(s)).

    It is difficult to tell without some sample data, but I'm guessing that none of the td elements have any child elements, and so it isn't selecting anything.

    Adjust the XPath to remove both of the predicate filters:

    //*[@id='DataTables_Table_0']/tbody/tr/td
    

    That should select all of the columns from all of the rows in that table.

    If that selects too many columns and you need to restrict it, provide some example content and describe what you want to select or exclude, and we can help you add an appropriate predicate filter.