Search code examples
rseleniumcss-selectorsrselenium

RSelenium: Click on the nth iteration of a class in a nested div


I'm trying to click on the 4th iteration of a svg of class "MuiSvgIcon-root.MuiSvgIcon-fontSizeMedium.css-10dohqv". You can see from the search bar at the bottom that this is the 4th of 16 times this class type appears in the code.

enter image description here

I figure this code should work:

logout <- remDr$findElement(using="css selector", value="svg.MuiSvgIcon-root.MuiSvgIcon-fontSizeMedium.css-10dohqv > :nth-of-type(4)")
logout$clickElement()

However, it's selecting a different element (the 5th). Further, R seems to think that this is the last element of this class type. If I use nth-of-type(5), I get the following error message Selenium message:Unable to locate element.

Any idea why this is happening or how to click on this element? Is the problem related to the existence of <div role="button" tabindex="0" class>?


Solution

  • I was able to click the button using findElements (plural) to get a list of the number of times this element occurred in the code, and then I selected the 5th element on the list. Note that I'm using a different css element (the list was shorter so it was just easier).

    logout <- remDr$findElements(using="css", value="button.MuiButtonBase-root._1rIDE.MuiIconButton-root.MuiIconButton-sizeMedium._1mHFW.css-1yxmbwk")
    logout[[5]]$clickElement()
    

    I'm dissatisfied with this because I don't understand why nth doesn't work. I also don't understand why this was the 5th element (under "Inspect", if I use control+F, this element only appears 4 times). But, this does work.