RSelenium: click on an invisible object - ElementNotVisibleException

The main menu of this page ( linio ) contains 11 links. Interested in only 9 (with a gray background and the display of the submenu when it hangs).

I want to click every single item in a submenu of 9 options. Desired process:

1.-First section: "Celulares y Tablets".
2.-Go to: "Celulares y Smartphones". Click on this page. 3.-Extract some data (verified, I was able to do this).

4.-Go to the next submenu in "Celulares y Tablets". What is: "Accesorios Celular".

5.-Extract some data and go to the next submenu. After completing all the submenus in this section, I will move on to the next large section: "TV-Audio-y-Foto".

And so on with 9 sections.

HTML structure

After looking at the source code, I came to the following:

1. Main heading: the main heading is in the tag "nav":

<nav id="headerMainMenu> 

2.- In the tag "nav" is "ul", and each "il" inside has an "id" for each of 9 sections:

 <nav id="headerMainMenu> <ul> <il id = "category-item-celulares-y-tablets"><a href="..."></il> <il id = "category-item-celulares-y-tablets"><a href="..."></il> <il id = "category-item-celulares-y-tablets"><a href="..."></il> </ul> </nav> 

3.- Inside the il elements there are div elements containing the links we need. Note the <a> with class = "subnav__title".

 <nav id="headerMainMenu> <ul> <il id = "category-item-celulares-y-tablets"><a href="..."> <div class="col-3"> <a href="..."class="subnav__title">TV y Video</a> </il> <il id = "category-item-celulares-y-tablets"><a href="..."></il> <il id = "category-item-celulares-y-tablets"><a href="..."></il> </ul> </nav> 

4.- Using RSelenium to go to each section:

 library(RSelenium) library(rvest) #start RSelenium checkForServer() startServer() remDr <- remoteDriver() remDr$open() #navigate to your page remDr$navigate("http://www.linio.com.pe/") #Accesing the first submenu from "Category Celulares y Tablets webElem <- remDr$findElement(using = 'css', value = "#category-item-celulares-y-tablets a.subnav__title") webElem$sendKeysToElement(list(key = "enter")) 

But this error is displayed:

 > webElem$sendKeysToElement(list(key = "enter")) Error: Summary: StaleElementReference Detail: An element command failed because the referenced element is no longer attached to the DOM. class: org.openqa.selenium.StaleElementReferenceException 

* I think this question may help. But I do not understand.

** I think my CSS is good.

+7
rselenium
source share
3 answers

I used the following code for Python. I am sure that it can be converted to your language:

 def click_hidden(self, css_selector): ''' Click on a hidden element using javascript. Selenium will error if the element doesn't excist and if javascript fails REASON: Selenium doesn't allow clicks on hidden elements since the user won't either So be sure the element would be visible in normal uses! ''' element = self.find_css(css_selector) self.execute_script("$(arguments[0]).click();", element) return element 
+1
source share

First you need to click on the parent menu. Then, when the submenu is visible, click the submenu.

 parentMenuElement <- remDr$findElement( using = 'css', value = "#category-item-celulares-y-tablets") parentMenuElement.click() childMenuElement <- remDr$findElement( using = 'css', value = "#category-item-celulares-y-tablets a.subnav__title") childMenuElement.click() 

You may also need to reject the modal popup that appears.

+1
source share

If any of your parent element of the element in question has the attribute "display: invisible", then its entire child element will be invisible to selenium, so you have to crack this script using JavaScript and click on it using Javascript click, Note. This can have adverse effects.

0
source share

All Articles