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.

3 Answers 3

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the try, @MatZeg. I think Chui has the answer. I need to test it to give him full credit. But thanks!
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.

7 Comments

Thanks Chui, I'll try this. May you post and update containing the part about the popup? How to dismiss it? Thanks!
Unfortunately, the pop up didn't appear again for me. You should find the [x] button by CSS, and if present click on it.
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, 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.
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...
|
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.