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

前端 未结 4 1164
无人共我
无人共我 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:43

    you need to use find_element_by_xpath not find_elements_by_xpath that return a list

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

    Also i think is better to use Waits for example.

    from selenium.webdriver.common.by import By    
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.firefox.options import Options
    
    options = Options()
    options.add_argument("--window-size=1920,1080")
    driver = webdriver.Firefox(firefox_options=options)
    
    path = "/html/body/div[5]/section/div[8]/div[1]/a[1]"
    try:
        element = WebDriverWait(driver, 5).until(
                               EC.element_to_be_clickable((By.XPATH, path)))
        element.click()
    finally:
        driver.quit()
    

提交回复
热议问题