AttributeError: 'list' 对象没有使用 Selenium 和 Python 的属性

AttributeError: #39;list#39; object has no attribute #39;click#39; using Selenium and Python(AttributeError: list 对象没有使用 Selenium 和 Python 的属性 click)
本文介绍了AttributeError: 'list' 对象没有使用 Selenium 和 Python 的属性 'click'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我想在默认设置为季度"的页面上单击年度"按钮.有两个链接基本上被称为相同,除了一个有 data-ptype="Annual" 所以我尝试复制 xpath 以单击按钮(也尝试了其他选项,但没有一个有效).

I'd like to click the button 'Annual' at a page that is by default set on 'Quarterly'. There are two links that are basically called the same, except that one has data-ptype="Annual" so I tryed to copy the xpath to click the button (also tried other options but none did work).

但是,我得到 AttributeError: 'list' object has no attribute 'click'.我阅读了很多类似的帖子,但无法解决我的问题..所以我认为 javascript 事件必须以某种方式被调用/单击/执行.. idk 我卡住了

However, I get the AttributeError: 'list' object has no attribute 'click'. I read a lot of similar posts, but wasn't able to fix my problem.. so I assume that javascript event must be called/clicked/performed somehow differnt.. idk Im stuck

from selenium import webdriver
link = 'https://www.investing.com/equities/apple-computer-inc-balance-sheet'

driver = webdriver.Firefox()
driver.get(link)
elm = driver.find_elements_by_xpath("/html/body/div[5]/section/div[8]/div[1]/a[1]").click()

html 如下:

<a class="newBtn toggleButton LightGray" href="javascript:void(0);" data-type="rf-type-button" data-ptype="Annual" data-pid="6408" data-rtype="BAL">..</a>

推荐答案

我仍然建议你使用 linkText 而不是 XPATH.这个 xpath 的原因: /html/body/div[5]/section/div[8]/div[1]/a[1] 非常绝对,如果有 可能会失败又一个 div 添加从 HTML 中删除.而更改链接文本的机会非常小.

I would still suggest you to go with linkText over XPATH. Reason this xpath : /html/body/div[5]/section/div[8]/div[1]/a[1] is quite absolute and can be failed if there is one more div added or removed from HTML. Whereas chances of changing the link Text is very minimal.

所以,而不是这个代码:

elm = driver.find_elements_by_xpath("/html/body/div[5]/section/div[8]/div[1]/a[1]").click()

试试这个代码:

annual_link = driver.find_element_by_link_text('Annual')
annual_link.click()

是的,@Druta 是对的,将 find_element 用于一个 Web 元素,将 find_elements 用于 Web 元素列表.显式等待总是好的.

and yes @Druta is right, use find_element for one web element and find_elements for list of web element. and it is always good to have explicit wait.

像这样创建显式等待的实例:

Create instance of explicit wait like this :

wait = WebDriverWait(driver,20)

并像这样使用等待引用:

and use the wait reference like this :

wait.until(EC.elementToBeClickable(By.LINK_TEXT, 'Annual'))  

更新:

from selenium import webdriver
link = 'https://www.investing.com/equities/apple-computer-inc-balance-sheet'

driver = webdriver.Firefox()
driver.maximize_window()
wait = WebDriverWait(driver,40)
driver.get(link)  

driver.execute_script("window.scrollTo(0, 200)") 

wait.until(EC.element_to_be_clickable((By.LINK_TEXT, 'Annual')))
annual_link = driver.find_element_by_link_text('Annual')
annual_link.click()
print(annual_link.text)  

确保导入这些:

from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC 

这篇关于AttributeError: 'list' 对象没有使用 Selenium 和 Python 的属性 'click'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

SCRIPT5: Access is denied in IE9 on xmlhttprequest(SCRIPT5:在 IE9 中对 xmlhttprequest 的访问被拒绝)
XMLHttpRequest module not defined/found(XMLHttpRequest 模块未定义/未找到)
Show a progress bar for downloading files using XHR2/AJAX(显示使用 XHR2/AJAX 下载文件的进度条)
How can I open a JSON file in JavaScript without jQuery?(如何在没有 jQuery 的情况下在 JavaScript 中打开 JSON 文件?)
quot;Origin null is not allowed by Access-Control-Allow-Originquot; in Chrome. Why?(“Access-Control-Allow-Origin 不允许 Origin null在铬.为什么?)
How to get response url in XMLHttpRequest?(如何在 XMLHttpRequest 中获取响应 url?)