他们给了我使用 selenium 的空输出

svgewumm  于 2022-11-10  发布在  其他
关注(0)|答案(1)|浏览(131)

我正在使用wait method查找元素,但它们给出的输出为空。错误出在哪里?
链接https://www.amazon.co.uk//dp/B094FZ1XFJ

from selenium import webdriver
    import time
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.wait import WebDriverWait
    from selenium.webdriver.support.select import Select

    PATH="C:\Program Files (x86)\chromedriver.exe"
    url='https://www.amazon.co.uk//dp/B094FZ1XFJ'
    driver =webdriver.Chrome(PATH)

    driver.get(url)

    item = dict()
    try:
        about_this_item_list = []
        about_this_item_divs = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@id='featurebullets_feature_div']//ul//li//span")))
        for div in about_this_item_divs:
            about_this_dict = dict()
            about_this_dict['about_this_item'] = div.text
            about_this_item_list.append(about_this_dict)
        item['about_this_item'] = about_this_item_list
    except:
        item['about_this_item'] = ''

    print(item)
dtcbnfnu

dtcbnfnu1#

它应该是visibility_of_all_elements_located(),它返回元素列表。

about_this_item_divs = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[@id='featurebullets_feature_div']//ul//li//span")))

这将仅返回Web元素而不是Web元素列表。

about_this_item_divs = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@id='featurebullets_feature_div']//ul//li//span")))

更新

您似乎需要滚动页面才能看到元素。尝试使用presence_of_all_elements_located()

about_this_item_divs = WebDriverWait(driver, 20).until(EC.presence_of_all_elements_located((By.XPATH, "//div[@id='featurebullets_feature_div']//ul//li//span")))

输出:

{'about_this_item': [{'about_this_item': 'Innovative design: Our clever car boot liners for dogs have been designed to be fully adaptable. The cover features a unique armrest pass-through and can be used with the back seats up, on a 60/40 split, a 40/60 split, a 40/20/40 split, or folded down all the way.'}, {'about_this_item': 'All-round protection: This multipurpose car boot liner shields your car from mud, dirt, fur, paw prints, spills, and more! It features extra high side panels with hook and loop fastenings to help keep the upholstery of your car in great condition.'}, {'about_this_item': 'Scratch- & slip-proof: Thanks to the non-slip backing and seat anchors, the boot cover for dogs stays put even on the ruffest rides! Worried your pup is going to scratch your boot or doors? Don’t be, our padded boot protector is completely claw- and scratch-proof.'}, {'about_this_item': 'Easy-care: Our waterproof dog car boot covers for cars protect your boot stays from unpleasant accidents or spills. The car boot cover is easy to spot clean with a damp cloth and can even be thrown into the washing machine on the gentle cycle!'}, {'about_this_item': ''}]}

相关问题