Answer a question

I have created a webscraper which could scrape data from a website such as the name of product,its price,description,item no etc. The scraper is being fed multiple web addresses of the same website( what this means is it is being fed with ebay.com/handbags ebay.com/perfumes ebay.com/cameras etc). My issue is if a certain website say ebay.com/handbags has a column 'RRP' it scrapes it, but if the website 'ebay.com/cameras' doesn't have an RRP the program fails for obvious reasons. The error reads as : selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="vi-priceDetails"]/span[1]/span[2]/span"}

How do I make sure that instead of failing the program, It should simply print a '-' in front of RRP? here is my code example:

import time
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager


def scrape_products():
    website_address = [
        'https://www.ebay.co.uk/itm/The-Discworld-series-Carpe-jugulum-by-Terry-Pratchett-Paperback-Amazing-Value/293566021594?hash=item4459e5ffda:g:yssAAOSw3NBfQ7I0',
        'https://www.ebay.co.uk/itm/Edexcel-AS-A-level-history-Germany-and-West-Germany-1918-89-by-Barbara/293497601580?hash=item4455d1fe2c:g:6lYAAOSwbRFeXGqL']
    options = webdriver.ChromeOptions()
    options.add_argument('start-maximized')
    options.add_experimental_option("excludeSwitches", ["enable-automation"])
    options.add_experimental_option("useAutomationExtension", False)

    browser = webdriver.Chrome(ChromeDriverManager().install(), options=options)
    for web in website_address:
        browser.get(web)
        time.sleep(2)

        product_rrp = browser.find_element_by_xpath('//*[@id="vi-priceDetails"]/span[1]/span[2]/span').text
       #rest of code
        print(product_rrp)


if __name__ == "__main__":
        scrape_products()

I am not sure how to solve this issue. Please help me out. Thanks!

Answers

note: I have changed the selector because your selector is not work in my PC

there are 2 ways, using try except block

try:
    product_rrp = browser.find_element_by_css_selector('.actPanel  div div:nth-child(2) span').text
    print(product_rrp)
except:
    print('no rpp')

or using find_element[s]_... (with s) and check if it has results

product_rrp = browser.find_elements_by_css_selector('.actPanel  div div:nth-child(2) span')
if product_rrp: # has results
   print(product_rrp[0].text) # notice the [0]
else:
   print('no rpp')
    
Logo

Python社区为您提供最前沿的新闻资讯和知识内容

更多推荐