0

I made the following code to scrape some website. A list of product code is itered on research bar with Selenium. If there is no result found (if driver.find_element_by_css_selector("div[class='search-did-you-mean']"):) i just clear the research bar to make another search. If there is some results (elif driver.find_element_by_css_selector("div[class='result-search']"):) I scrape it

Here is the code :

for product in product_list:
    inputElement = driver.find_element_by_id("q")
    inputElement.send_keys(product[0])
    inputElement.send_keys(Keys.ENTER)
    inputElement.click()
    time.sleep(5)
    if driver.find_element_by_css_selector("div[class='search-did-you-mean']"):
        time.sleep(5)
        clearResearch = driver.find_element_by_id("q")
        WebDriverWait(driver, 10).until_not(EC.visibility_of_element_located((By.ID, "overley")))
        clearResearch.send_keys(Keys.CONTROL + "a")
        clearResearch.send_keys(Keys.DELETE)
    elif driver.find_element_by_css_selector("div[class='result-search']"):
        time.sleep(5)
        item['price'] = driver.find_element_by_css_selector("span[class='sale-price']").text
        item['desc'] = driver.find_element_by_css_selector("h3[class='product-name']").text
        print(item)

There is no result for the first product code of the list, so it is cleared and a new code is given. Problem appears with the second item, there is results but my elif condition seems not understand as I get an Unable to locate element: div[class='search-did-you-mean'] error.

Do you know what is wrong with my code ? Thanks a lot

2 Answers 2

1

This is selenium behavior will throw exception if no element found, wrap it in try-except

first_product = None
try:
    first_product = driver.find_element_by_css_selector("div[class='search-did-you-mean']"
except: pass

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

1 Comment

Really better this way ! It all works fine now :) thanks a lot
1

You can use find_elements_by_css_selector and check if the returned list has elements in it

if driver.find_elements_by_css_selector("div[class='search-did-you-mean']"):
    #...
elif driver.find_elements_by_css_selector("div[class='result-search']"):
    #...

Comments

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.