gpt4 book ai didi

RSelenium:单击不可见对象 - ElementNotVisibleException

转载 作者:行者123 更新时间:2023-12-03 00:31:17 24 4
gpt4 key购买 nike

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

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

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

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

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

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

HTML结构

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

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

<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 没问题。

最佳答案

我在 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

关于RSelenium:单击不可见对象 - ElementNotVisibleException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32409851/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com