7

此页面的主菜单 ( linio ) 有 11 个链接。只对 9 感兴趣(灰色背景并在悬停时显示子菜单)。

我想从 9 个选项中单击子菜单中的每个元素。所需的过程是:

1.-第一部分:“Celulares y 片剂”。
2.-转到:“Celulares y 智能手机”。请单击并查看此页面。
3.-提取一些数据(检查,我已经能够做到这一点)。

4.-转到“Celulares y Tablets”中的下一个子菜单。即:“Accesorios Celular”。

5.-提取一些数据,然后进入下一个子菜单。完成本部分中的所有子菜单后,我将进入下一个大部分:“TV-Audio-y-Foto”。

以此类推,共有 9 个部分。

HTML 结构

查看源代码,我已经到了这个:

1.- 主标题:主标题位于“导航”标签内:

<nav id="headerMainMenu>

2.- 'nav' 标签里面是一个'ul',里面的每个'il' 都有9 个部分的每一个部分的'id':

<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.- 在 il 元素中,有 div 元素包含我们需要的链接:请注意<a>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.- 使用 RSelenium 转到每个部分:

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"))

但这样做会显示此错误:

> 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

*我认为这个问题可能会有所帮助。但我不明白。

**我认为我的 CSS 没问题。

4

3 回答 3

1

您需要先单击父菜单。然后当子菜单可见时,单击子菜单。

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()

您可能还需要关闭偶尔出现的模式弹出窗口。

于 2015-09-15T10:23:50.253 回答
1

我将以下代码用于 Python。我确信它可以转换为您的语言:

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
于 2015-09-15T07:38:49.680 回答
0

如果有问题的元素的任何父元素具有属性“display:invisible”,那么它的所有子元素都将对 selenium 不可见,因此您必须使用 JavaScript 破解此类场景并使用 Javascript 的点击单击它。注意:它可能会产生不利影响。

于 2015-09-08T15:09:59.660 回答