Python 使用Selenium在第一个元素后返回空值进行刮取

Python 使用Selenium在第一个元素后返回空值进行刮取,python,selenium,xpath,css-selectors,classname,Python,Selenium,Xpath,Css Selectors,Classname,我正在抓取一个网页,由于某些原因,它正确返回前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,但它返回以下内容(示例): 你知道为什么会这样吗

我正在抓取一个网页,由于某些原因,它正确返回前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,但它返回以下内容(示例):

你知道为什么会这样吗

下面是一段源代码:

要从类为的所有元素中提取文本,请使用,并且您必须导出所有元素的可见性(),您可以使用以下任一选项:

  • 使用
    类名

    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\u选择器

    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
    

请提及网页链接感谢您的帮助,但我遇到了以下错误:回溯(最近一次调用):打印([my_elem.get_属性(“innerHTML”),用于WebDriverWait(driver,20)中的my_elem.get_属性)。直到(EC.visibility_of_all_element位于((By.CLASS_NAME,“offerList item description title”))文件“D:\Programas\Python\lib\site packages\selenium\webdriver\support\wait.py”,第80行,在until raise TimeoutException(message,screen,stacktrace)selenium.common.exceptions.TimeoutException:message:I已更改
get_属性(“innerHTML”)
.text
。请使用xpath和css重新测试,并让我知道状态。DebanjanB正在运行。非常感谢!您是否建议始终使用此方法将元素存储在列表中?还是仅在不可见元素的情况下?@FoxWox理想情况下,您必须始终将webdriver实例和浏览器实例保持在sy中nc使用硒进行活动。因此,您需要诱导适当类型的硒
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC