在第一个元素之后用 Selenium 刮擦返回空值

Scraping with Selenium returning empty values after first elements

我正在抓取一个网页,出于某种原因,它 returns 正确地 returns 前 12 个元素而不是剩余的 24 个元素,页面中总共显示了 36 个元素。

search_names = driver.find_elements_by_class_name('offerList-item-description-title')
names = []
for name in search_names:
    names.append(name.text)

search_names 的长度为 36,但 returns 以下(示例):

[1 , 2, 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , '', '', ... , '']

知道为什么会发生这种情况吗?

这是源代码的片段:

从所有具有 class 的元素中提取文本作为 offerList-item-description-title 使用 and you have to induce for visibility_of_all_elements_located() and you can use either of the following :

  • 使用CLASS_NAME:

    print([my_elem.text for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CLASS_NAME, "offerList-item-description-title")))])
    
  • 使用CSS_SELECTOR:

    print([my_elem.text for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "div.offerList-item-description-title")))])
    
  • 使用XPATH:

    print([my_elem.text for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[@class='offerList-item-description-title']")))])
    
  • 注意:您必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC