How to Fix This Error in Selenium: Attributeerror: 'List' Object Has No Attribute 'Find_Elements_By_Css_Selector'

How to fix this error in selenium: AttributeError: 'list' object has no attribute 'find_elements_by_css_selector'

find_elements_by_css_selector('a') will returns as list not element.

Change this line.

link.find_elements_by_css_selector('a').get_attribute('href')

to

link.find_element_by_css_selector('a').get_attribute('href')

Edited:

URL ="https://www.bing.com/search?q=site%3Alocalbitcoins.com&qs=n&sp=-1&pq=site%3Alocalbitcoins.com&sc=0-22&sk=&cvid=D38F613A00C64A88B2C0F87BD653088A&first=0"
driver.get(URL)
for link in driver.find_elements_by_css_selector("h2>a"):
print(link.text + "," + link.get_attribute('href'))

AttributeError: 'list' object has no attribute 'click' - Selenium Webdriver

Thanks for helping out. I found the answer for myself. Idea given by "Dan Niero"

The problem is, I am using driver.find_element[s] instead of driver.find_element. So one s makes difference and calling a wrong method. In fact I am following the eclipse autocomplete :(. Obviously driver.find_elements_by_link_text returns list so If I send click event it wont understand.

Thanks for helping and sorry for my bad question

-Vikram

Selenium 'list' object has no attribute 'text'

The error appear because this line:

.find_elements_by_xpath('/html/body/div[1]/div[2]/div/div/div/div[2]/div[1]/main/div[3]/div/div[1]/div[1]/div[1]/a/div/div[4]/span[2]').text

The above return a list.

.text method utilize to .find_element_* (without s)

But in simple you can scrape the title by css selector with this value : div[data-comp="ProductDisplayName "] span[data-at="sku_item_name"]

Try following code:

url = 'https://www.sephora.com/ca/en/shop/face-makeup'

driver.get(url)
time.sleep(2)

browser = scrollDown(driver, 20)

titles = driver.find_elements_by_css_selector('div[data-comp="ProductDisplayName "] span[data-at="sku_item_name"]')
for title in titles:
print(title.text)

To scrape the brand, you only change selector to be : div[data-comp="ProductDisplayName "] span[data-at="sku_item_brand"]

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

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


Related Topics



Leave a reply



Submit