AttributeError: 'list' object has no attribute 'click' using Selenium and Python

前端 未结 4 1151
无人共我
无人共我 2020-12-19 13:12

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

4条回答
  •  孤城傲影
    2020-12-19 13:51

    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.

    So, Instead of this code :

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

    try this code :

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

    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'))  
    

    UPDATE:

    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)  
    

    make sure to import these :

    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 
    

提交回复
热议问题