无法单击()带有 selenium 的 onclick 元素(尝试过的文本链接、部分文本链接、xpath、css 选择器)

德拉卡克斯

我需要从这个网址中删除一些数据:https ://www.cnrtl.fr/definition/coupe

我需要废弃的数据/结果位于这 3 个不同的选项卡中:在此处输入图像描述

我无法单击 onclick 元素,它应该让我从一个选项卡切换到另一个选项卡。

这里是 3 个 onclick 元素之一的 html 代码:在此处输入图像描述

3 个 onclick 元素的不同之处在于末尾的数字:

#COUPE1:
return sendRequest(5,'/definition/coupe//0');

#COUPE2:
return sendRequest(5,'/definition/coupe//1');

#COUPER:
return sendRequest(5,'/definition/coupe//2');

我试图通过链接文本、部分链接文本、xpath 和 css 选择器找到它们。

我关注了这个线程:Python + Selenium:如何点击“onclick”元素?

还可以尝试 contains 和 text() 方法。

没有成功。

卢坎

有几种方法可以做到这一点。我选择了我所做的方法,因为页面重新加载导致元素变得陈旧。

#Get the URL
driver.get("https://www.cnrtl.fr/definition/coupe")
#Find the parent element of the tabs
tabs = driver.find_element(By.ID, 'vtoolbar')
#Get all the list items under the parent (tabs)
lis = tabs.find_elements(By.TAG_NAME, 'li')
#loop over them (skipping the first tab, because that's already loaded)
for i in range(1, len(lis)):
    #Execute the same JS as the page would on click, using the index of the loop
    driver.execute_script(f"sendRequest(5,'/definition/coupe//{i}');")
    #Sleep to visualise the clicking
    time.sleep(3)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章