如果Elif问题/ Selenium Python

时间:2018-12-11 10:41:19

标签: python selenium

我做了以下代码来抓取一些网站。使用硒在研究栏上迭代产品代码列表。如果没有找到结果(if driver.find_element_by_css_selector("div[class='search-did-you-mean']"):),我只需清除研究栏即可进行其他搜索。如果有结果(elif driver.find_element_by_css_selector("div[class='result-search']"):)我将其刮掉

这是代码:

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)

该列表的第一个产品代码没有结果,因此将其清除并给出新的代码。问题出现在第二项中,有结果,但是我得到一个无法定位元素:div [class ='search-did-you-mean'] 错误时,我的elif条件似乎无法理解。 / p>

您知道我的代码有什么问题吗?非常感谢

2 个答案:

答案 0 :(得分:1)

这是硒行为,如果找不到任何元素,将引发异常,将其包装在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:
    .....

答案 1 :(得分:1)

您可以使用find_elements_by_css_selector并检查返回的列表中是否包含元素

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