如何使用Selenium跳过Python中的"Unable to find element with class name"错误



下面的代码完成了我需要它做的事情,除非它遇到一个缺少class_name的产品,比如product-price。我需要帮助的是如何跳过那个特定的项目,进入下一个项目。目前我得到以下错误:

"selenium.com .exceptions. nosuchelementexception: Message: {"errorMessage":"无法找到类名为" product-display-price "的元素","request":{"headers":{"Accept":"application/json","Accept- encoding ":"identity","Connection":"close","Content-Length":"138","Content-Type":"application/json;charset=UTF-8","Host":"127.0.0.1:64186","User-Agent":"Python-urllib/3.5"},"httpVersion":"1.1","method":"POST"," POST":"{"id": ":wdc:1473855489054", "value":product-display-price "","","类名","sessionId ","48018400 - 7 - a75 - 11 - e6 b0ab - 5 - f6a864b5c88 "}"、"url":"/"、"urlParsed元素":{"锚":","查询":","文件":"元素","目录":"/"、"路径":"/元素","相对":"/元素","港":","主机":","密码":","用户":" ","用户信息":" ","权威":","协议":","源":"/元素","queryKey":{},"块":["元素"]},"urlOriginal":"/会议/48018400 - 7 - a75 11 - e6 b0ab - 5 - f6a864b5c88/元素/:wdc: 1473855489054/元素"}}
import csv
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
b = open('csv/homedepotfridges.csv', 'w', newline='')
a = csv.writer(b,delimiter=',')
driver = webdriver.PhantomJS()
driver.get('https://www.homedepot.ca/en/home/categories/appliances/dishwashers.html#!p=0&q=*%3Aprice-asc%3AcategoryPathHierarchy%3A2%2Fhd-classes%2Fl1-appliances%2Fl2-dishwashers')
items = []
for item in driver.find_elements_by_class_name('item'):
    try:
        model = item.find_element_by_class_name('product-model')
        price = item.find_element_by_class_name('product-display-price')
        title = item.find_element_by_class_name('product-title')
        url = item.find_element_by_class_name('js-detail-link')
        items.append({'model': model, 'price': price, 'title': title, 'url': url})
        print (model.text, price.text, title.text, url.get_attribute("href"))
        c = (model.text, price.text, title.text, url.get_attribute("href"))
        a.writerow(c)
    except NoSuchElementException:
        model = 'n/a'
        price = 'N/A'
        title = 'N/A'
        url = 'N/A'
        items.append({'model': model, 'price': price, 'title': title, 'url': url})
        print(model.text, price.text, title.text, url.get_attribute("href").text)
        c = (model.text, price.text, title.text, url.get_attribute("href"))
    a.writerow(c)

b.close()

Traceback(最近一次调用):文件"/Users/User/PycharmProjects/Test/homedepotdishwasher .py",第31行打印(模型。文本、价格。文本、标题。文本,url.get_attribute.text("href"))AttributeError: 'str'对象没有属性'text'

感谢您的帮助

有几种方法。你试过吗?

1)用try包围行-除了https://docs.python.org/3/tutorial/errors.html

from selenium.common.exceptions import NoSuchElementException
for item in driver.find_elements_by_class_name('item'):
    try:
        model = item.find_element_by_class_name('product-model')
        price = item.find_element_by_class_name('product-display-price')
        title = item.find_element_by_class_name('product-title')
        url = item.find_element_by_class_name('js-detail-link')
        items.append({'model': model, 'price': price, 'title': title, 'url': url})
        print (model.text, price.text, title.text, url.get_attribute("href"))
        c = (model.text, price.text, title.text, url.get_attribute("href"))
        a.writerow(c)
    except NoSuchElementException:
        #here you can do what you want to do when an element is not found. Then it'll continue with the next one.
b.close()

2)使用if语句检查是否找到该项。为此,您可以创建一个包含所有这些元素的列表(find_elements_by_…),并查看该列表的长度是否大于0。如:

最新更新