错误有时会发生,有时会发生在不同的地方

时间:2016-09-14 23:39:09

标签: python-3.x selenium-webdriver

以下错误会不时弹出,并不总是在同一个地方。不确定为什么。有线索吗?完整代码:Full Code

  

文件“/Users/davidkasapchuk/PycharmProjects/Test/HomeDepotFridges.py”,第257行,       price = item.find_element_by_class_name('product-display-price')     在find_element_by_class_name中输入文件“/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium/webdriver/remote/webelement.py”,第264行       return self.find_element(by = By.CLASS_NAME,value = name)     在find_element中输入文件“/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium/webdriver/remote/webelement.py”,第478行       {“使用”:by,“value”:value})['value']     在_execute中输入文件“/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium/webdriver/remote/webelement.py”,第461行       return self._parent.execute(command,params)     文件“/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py”,第236行,执行       self.error_handler.check_response(响应)     在check_response中输入文件“/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium/webdriver/remote/errorhandler.py”,第192行       提出exception_class(消息,屏幕,堆栈跟踪)   selenium.common.exceptions.NoSuchElementException:消息:{“errorMessage”:“无法找到具有类名的元素'product-display-price'”,“request”:{“headers”:{“Accept”:“application / json “ ”接受编码“: ”同一性“, ”连接“: ”关闭“, ”内容长度“: ”138“, ”内容类型“: ”应用/ JSON;字符集= UTF-8“,” 主机“:” 127.0.0.1:52314" , “用户代理”: “Python的的urllib / 3.5”}, “httpVersion”: “1.1”, “方法”: “POST”, “后”: “{\” ID \“:\”:wdc:1473895291967 \“,\”使用\“:\”类名\“,\”sessionId \“:\”eb417d30-7ad1-11e6-82c2-d52d6cd5b804 \“,\”value \“ :\“product-display-price \”}“,”url“:”/ element“,”urlParsed“:{”anchor“:”“,”query“:”“,”file“:”element“,”目录 “:”/”, “路径”: “/元件”, “相对的”: “/元件”, “端口”: “”, “宿主”: “”, “密码”: “”, “用户”: “”, “用户信息”: “”, “权威”: “”, “协议”: “”, “源”: “/元件”, “queryKey”:{}, “块”:[ “元件”]} “urlOriginal”: “/会话/ eb417d30-7ad1-11e6-82c2-d52d6cd5b804 /元件/:WDC:1473895291967 /元件”}}   屏幕截图:可通过屏幕获取

     

在处理上述异常期间,发生了另一个异常:

     

追踪(最近一次通话):     文件“/Users/User/PycharmProjects/Test/HomeDepotFridges.py”,第266行,       model ='n / a'.text   AttributeError:'str'对象没有属性'text'>

import csv
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
import time

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/refrigerators-and-freezers/refrigerators.html')
time.sleep(15)
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'.text
    price = 'N/A'.text
    title = 'N/A'.text
    url = 'N/A'.text
    items.append({'model': model, 'price': price, 'title': title, 'url': url})
    print(model.text, price.text, title.text, url.get_attribute.text("href"))
    c = (model.text, price.text, title.text, url.get_attribute("href"))
    a.writerow(c)
b.close()

b = open('csv/homedepotfridges.csv', 'a', newline='')
a = csv.writer(b,delimiter=',')

driver = webdriver.PhantomJS()
driver.get('https://www.homedepot.ca/en/home/categories/appliances/refrigerators-and-freezers/refrigerators.html#!p=1&q=*%3Arelevance%3AcategoryPathHierarchy%3A3%2Fhd-classes%2Fl1-appliances%2Fl2-refrigerators-freezers%2F1010215')
time.sleep(15)
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'.text
        price = 'N/A'.text
        title = 'N/A'.text
        url = 'N/A'.text
        items.append({'model': model, 'price': price, 'title': title, 'url': url})
        print(model.text, price.text, title.text, url.get_attribute.text("href"))
        c = (model.text, price.text, title.text, url.get_attribute("href"))
        a.writerow(c)
b.close()

1 个答案:

答案 0 :(得分:0)

您应该使用某种等待来等待元素加载,然后再对它们执行操作。这可以解决你的问题。