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
How to Transform Floats to Integers in a List
How to Insert Text At Line and Column Position in a File
_Corrupt_Record Error When Reading a Json File into Spark
How Do Convert a Pandas Dataframe to Xml
How to Display a Float With Two Decimal Places
Parsing a Pipe-Delimited File in Python
Windowserror: [Error 193] %1 Is Not a Valid Win32 Application in Python
How to Display Index During List Iteration With Django
How to Select Percentage of Rows in Pandas Dataframe
Sys.Path Different in Jupyter and Python - How to Import Own Modules in Jupyter
Get Business Days Between Start and End Date Using Pandas
How to Find the Most Common Element in the List of List in Python
Add Excel File Attachment When Sending Python Email
Create New Column Based on String
How to Convert Np.Int64 into Python Int64 for Pandasseries
Using Regex to Find All Phrases That Are Completely Capitalized