2015-09-05 66 views
7

此页面的主菜单(linio)有11个链接。只对9(灰色背景和徘徊时显示子菜单)感兴趣。RSelenium:单击不可见对象 - ElementNotVisibleException

我想单击9个选项中的子菜单中的每个单个元素。期望的方法是:

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

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

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

等9个部分。

HTML Estructure

寻找源代码,我到了这个:

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>与类= “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 

*我觉得这question可能是帮帮我。但我不明白。

**我认为我的CSS是好的。

回答

0

如果有问题的元素的任何父元素具有属性'display:invisible',那么它的所有子元素对于selenium都是不可见的,因此您必须使用JavaScript来破解这种场景,然后使用Javascript单击它单击。注意:它可能有不利影响。

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 
+0

感谢您的试用,@MatZeg。我认为翠有答案。我需要测试它才能给他充分的信用。但是,谢谢! –

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

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

+0

谢谢Chui,我会试试这个。你可以发布并更新包含关于弹出窗口的部分吗?如何解雇它?谢谢! –

+0

不幸的是,弹出窗口对我来说并没有再次出现。你应该通过CSS找到[x]按钮,如果有的话点击它。 –

+0

嗨Chui,我不明白为什么需要点击主菜单?在普通的浏览器中,它是在“悬停”而不是“点击”时激活的。请你可以解释一下吗? –