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

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'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在默认设置为季度"的页面上单击年度"按钮.有两个链接基本上被称为相同,除了一个有 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'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:AttributeError: 'list' 对象没有使用 Selenium 和 Python 的属性 'click'

基础教程推荐