开发者

"TypeError: 'function' object is not iterable", why am I getting this error?

from selenium import webdriver

from selenium.webdriver.common.by import By
from selenium.webdr开发者_Go百科iver.support.ui import WebDriverWait as wait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from selenium. webdriver. chrome. options import Options

web = 'https://www.amazon.com'
driver_path = 'V:\Python Project\chromedriver_win32\chromedriver.exe'

options = webdriver.ChromeOptions()
options.add_argument('--headless')

s=Service('V:\Python Project\chromedriver_win32\chromedriver.exe')
driver = webdriver.Chrome(service=s)

driver.maximize_window()  # For maximizing window
driver.implicitly_wait(30)
# driver = webdriver.Chrome(options=options, executable_path=driver_path)
driver.get(web)

driver.implicitly_wait(5)
keyword = "Table"

search = driver.find_element(By.ID,"twotabsearchtextbox")
search.send_keys(keyword)
# click search button
search_button = driver.find_element(By.ID,'nav-search-submit-button')
search_button.click()

driver.implicitly_wait(5)

product_asin = []
product_name = []
product_price = []
product_ratings = []
product_ratings_num = []
product_link = []

items =(EC.presence_of_all_elements_located((By.CLASS_NAME, "s-result-item sasin"))) *=>Error on this line*

for item in items:
    # find name
    name = item.find_element(By.CLASS_NAME,"a-size-medium a-color-base a-text-normal")
    product_name.append(name.text)

    # find ASIN number
    data_asin = item.get_attribute("data-asin")
    product_asin.append(data_asin)

    # find price
    whole_price = item.find_element(By.CLASS_NAME,"a-price-whole")
    fraction_price = item.find_element(By.CLASS_NAME,"a-price-fraction")

    if whole_price != [] and fraction_price != []:
        price = '.'.join([whole_price[0].text, fraction_price[0].text])
    else:
        price = 0
    product_price.append(price)

    # find ratings box
    ratings_box = item.find_element(By.CLASS_NAME,"a-row a-size-small")

    # find ratings and ratings_num
    if ratings_box != []:
        ratings = ratings_box[0].get_attribute('aria-label')
        ratings_num = ratings_box[1].get_attribute('aria-label')
    else:
        ratings, ratings_num = 0, 0

    product_ratings.append(ratings)
    product_ratings_num.append(str(ratings_num))

    # find link
    link = item.find_element(By.CLASS_NAME,"a-link-normal a-text-normal").get_attribute("href")
    product_link.append(link)

driver.quit()

# to check data scraped
print(product_name)
print(product_asin)
print(product_price)
print(product_ratings)
print(product_ratings_num)
print(product_link)

For the code above I am getting the following error:

for item in items:
TypeError: 'function' object is not iterable

I am working on the above code, but it is giving me the error, "TypeError: 'function' object is not iterable". It should be working fine, as it is mostly correct, but don't know what is missing, which gives the error. Please can anyone provide mw with the solution?.


EC.presence_of_all_elements_located

this function will not return anything it will only check is the item which you want is available or not. Use something like below

WebDriverWait(self.browser, 5).until(
        EC.visibility_of_all_elements_located((By.CLASS_NAME, "s-result-item sasin")))

    rows = self.browser.find_elements(
        By.CLASS_NAME, "s-result-item sasin")

    self.assertIn(
        rowItem,
        [row.text for row in rows]
    )

I think there is some mistake in class can you check below code

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from selenium. webdriver. chrome. options import Options

web = 'https://www.amazon.com'
driver_path = 'chromedriver_win32\chromedriver.exe'

options = webdriver.ChromeOptions()
options.add_argument('--headless')

# ser=Service(executable_path='chromedriver_win32\chromedriver.exe')
driver = webdriver.Chrome(executable_path='chromedriver_win32\chromedriver.exe')

driver.maximize_window()  # For maximizing window
driver.implicitly_wait(30)
# driver = webdriver.Chrome(options=options, executable_path=driver_path)
driver.get(web)

driver.implicitly_wait(5)
keyword = "Table"

search = driver.find_element(By.ID,"twotabsearchtextbox")
search.send_keys(keyword)
# click search button
search_button = driver.find_element(By.ID,'nav-search-submit-button')
search_button.click()

driver.implicitly_wait(5)

product_asin = []
product_name = []
product_price = []
product_ratings = []
product_ratings_num = []
product_link = []

EC.presence_of_all_elements_located((By.CLASS_NAME, "s-result-item"))

rows = driver.find_elements(
        By.CLASS_NAME, "s-result-item")

print(rows)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜