Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

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

The html is the following:

<a class="newBtn toggleButton LightGray" href="javascript:void(0);" data-type="rf-type-button" data-ptype="Annual" data-pid="6408" data-rtype="BAL">..</a>
like image 393
Felix Avatar asked Jul 12 '18 13:07

Felix


People also ask

How do you select a button in selenium Python?

We can click a button with Selenium webdriver in Python using the click method. First, we have to identify the button to be clicked with the help of any locators like id, name, class, xpath, tagname or css. Then we have to apply the click method on it. A button in html code is represented by button tagname.

What are attributes in selenium?

Selenium's getAttribute() method is used to get attributes of HTML elements under test. This method is a part of the WebElement Class. The getAttribute() method can be used for getting values for different attributes like class, name,src, CSS style value, etc.

What is by class in selenium WebDriver?

class() Method. This method makes it possible to locate an element by referencing its class name. The class() method is found inside the By class of the Selenium WebDriver JavaScript library. The class also contains other alternative methods for locating elements.


1 Answers

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()
like image 85
Druta Ruslan Avatar answered Sep 28 '22 07:09

Druta Ruslan