7

The main menu of this page (linio) has 11 links. Only interested in 9 (those with gray background and show submenus when hovered).

I want to click every single element in the submenu from the 9 options. The desired process is:

1.-First section: "Celulares y Tablets".
2.-Go to: "Celulares y Smartphones". Do click and see this page.
3.-Extract some data (checked, I've been able to do this).

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

5.-Extract some data, and go to the next submenu. After done with all the submenus in this section, I would go to the next big section: "TV-Audio-y-Foto".

And so on with the 9 sections.

HTML Estructure

Looking the source code, I've arrived to this:

1.- Main Header: the main header is within a 'nav' tag:

<nav id="headerMainMenu>

2.- Inside the 'nav' tag is a 'ul', and every 'il' inside has and 'id' for each one of the 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: Please, notice the <a> with the 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 doing so shows this error:

> 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 could be of help. But I don't get it.

**I think my CSS is Okay.

Community
  • 1
  • 1
Omar Gonzales
  • 3,806
  • 10
  • 56
  • 120

3 Answers3

1

I used the following code for Python. I'm sure 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
MatZeg
  • 448
  • 1
  • 4
  • 12
1

You need to click on the parent menu first. Then when the submenu is visible, click on 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 dismiss the modal popup that occasionally appears.

Chui Tey
  • 5,436
  • 2
  • 35
  • 44
  • Thanks Chui, I'll try this. May you post and update containing the part about the popup? How to dismiss it? Thanks! – Omar Gonzales Sep 19 '15 at 02:15
  • Unfortunately, the pop up didn't appear again for me. You should find the [x] button by CSS, and if present click on it. – Chui Tey Sep 19 '15 at 06:37
  • Hi Chui, I don't understand why one needs to click on the main menu? In a normal browser it is activated on "hover" not "click". Please, may you give an explanation? – Omar Gonzales Sep 26 '15 at 00:38
  • Omar, you'll have to resort to tricky javascript to simulate mouse hover, or simulate a click first as Selenium will not click on your non-visible menu element. Using click is easier. – Chui Tey Sep 26 '15 at 00:50
  • But "clicking" is the same behaviur as "hover"? Cause, in Chrome, when clicking, it takes you to the section, and the submenu does not appear more... – Omar Gonzales Sep 26 '15 at 00:55
  • In which case you'll need to resort to tricky Javascript. There are some Stackoverflow posts that address this. – Chui Tey Sep 26 '15 at 02:06
  • Hi @Chui I've tasted (finally) your code and you have a typo. It should be `childMenuElement$click()` not `childMenuElement.click()`. Thanks for your answer. – Omar Gonzales Sep 29 '15 at 15:53
0

If any of your parent element of the element in question has attribute 'display: invisible' then all of its child element will be invisible to selenium, so you will have to hack such scenario with JavaScript and click on it using Javascript's click. Note: It may have adverse affects.

Mrunal Gosar
  • 4,595
  • 13
  • 48
  • 71