此页面的主菜单 ( 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 没问题。