1

The following code does what I need it to do except when it comes across a product that is missing a class_name, lets say product-price. What I need help with is how to skip over that particular item and move onto the next one. Currently I get the following error :

"selenium.common.exceptions.NoSuchElementException: Message: {"errorMessage":"Unable to find element with class name '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\", \"using\": \"class name\", \"sessionId\": \"48018400-7a75-11e6-b0ab-5f6a864b5c88\"}","url":"/element","urlParsed":{"anchor":"","query":"","file":"element","directory":"/","path":"/element","relative":"/element","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/element","queryKey":{},"chunks":["element"]},"urlOriginal":"/session/48018400-7a75-11e6-b0ab-5f6a864b5c88/element/:wdc:1473855489054/element"}}"
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 (most recent call last): File "/Users/User/PycharmProjects/Test/HomeDepotDishwashers.py", line 31, in print(model.text, price.text, title.text, url.get_attribute.text("href")) AttributeError: 'str' object has no attribute 'text'

Any help is appreciated

0

1 Answer 1

0

Several ways to do that. Have you tried any?

1) surround the line with a try - except 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) use an if statement to check if this item is found. You do this by making a list of all those elements (find_elements_by_...) and see if the length of that list is greater than 0. eg:

Sign up to request clarification or add additional context in comments.

3 Comments

I have tried the if statement but what I need is the statement to be able to see if there is nothing there to continue on to the next product and so on.
so put all of the stuff you may want to skip inside the if. Then with the if check if all the elements are found. Let me update my answer.
OK, so I have edited the code above to include to Element Exception. What I need to do is to write to a CSV file. I don't really care if the product if there are 'N/A' is the file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.