AttributeError:“列表"对象使用Selenium和Python没有属性“单击" [英] AttributeError: 'list' object has no attribute 'click' using Selenium and Python
问题描述
我想在默认设置为季度"的页面上单击年度"按钮.基本上有两个链接被称为相同的链接,除了一个链接具有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 Im卡住了
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>
推荐答案
我仍然建议您通过 XPATH 使用 linkText .此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元素的列表.拥有explicit wait
总是很好.
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:“列表"对象使用Selenium和Python没有属性“单击"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!