如何使用 Selenium 将鼠标悬停在多个元素上并通过 Python 提取文本

How to mouse hover multiple elements using Selenium and extract the texts through Python

我目前正在尝试抓取一个动态网站,该网站使用 javascript 在将鼠标悬停在图像上后提供信息。我试图通过将鼠标悬停在这些图像上来获取文本容器内的数据,但是这样做很困难,因为当我定义所有元素时,然后尝试创建一个悬停在所有元素上的循环,我只从我定义的第一个元素接收文本数据,一遍又一遍地接收尽可能多的元素(本页有 10 个)。这里有一些代码可以帮助您重现我的问题。我是否需要在此循环中引入等待以生成正确的结果?谢谢

from selenium import webdriver
driver = webdriver.Chrome(r'C:\Users\Hank\Desktop\chromedriver_win32\chromedriver.exe')
driver.get('https://steamcommunity.com/market/listings/440/Unusual%20Old%20Guadalajara')
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.expected_conditions import presence_of_element_located
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions as EC
action = ActionChains(driver)
imgs = driver.find_elements(By.CSS_SELECTOR, '[class^=market_listing_item_img][id^=listing_]')
for item in imgs:
    action.move_to_element(img).perform()
    descriptors = driver.find_element(By.CLASS_NAME, 'item_desc_descriptors')
    print(descriptors.text)

代码然后 return 正是我想要的第一个元素。感谢您抽出宝贵时间,如果网站上的其他地方有我的问题的答案,请告诉我,我已经看过但似乎找不到。

over multiple elements using you need to induce for visibility_of_all_elements_located() and you can use either of the following

  • 使用CSS_SELECTOR:

    driver.get('https://steamcommunity.com/market/listings/440/Unusual%20Old%20Guadalajara')
    elements = WebDriverWait(driver, 5).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "img.market_listing_item_img.economy_item_hoverable")))
    for element in elements:
        ActionChains(driver).move_to_element(element).perform()
        print([my_elem.text for my_elem in WebDriverWait(driver, 5).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "div.item_desc_description div.item_desc_descriptors#hover_item_descriptors div.descriptor")))])
    
  • 使用XPATH:

    elements = WebDriverWait(driver, 5).until(EC.visibility_of_all_elements_located((By.XPATH, "//img[@class='market_listing_item_img economy_item_hoverable']")))
    for element in elements:
        ActionChains(driver).move_to_element(element).perform()
        print([my_elem.text for my_elem in WebDriverWait(driver, 5).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[@class='item_desc_description']//div[@class='item_desc_descriptors' and @id='hover_item_descriptors']//div[@class='descriptor']")))])
    
  • 注意:您必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.action_chains import ActionChains
    
  • 控制台输出:

    ['Paint Color: A Distinctive Lack of Hue', '★ Unusual Effect: Dead Presidents', "''It was opened with (Crate Depression 2019) as the first''"]
    ["★ Unusual Effect: Nuts n' Bolts", 'This hat adds spice to any occasion.']
    ['★ Unusual Effect: Orbiting Fire', 'This hat adds spice to any occasion.', 'Tradable After: Friday, June 19, 2020 (7:00:00) GMT']
    ['★ Unusual Effect: Bubbling', 'This hat adds spice to any occasion.', 'Gift from: Tombusken', 'Date Received: Sunday, December 22, 2013 (22:07:14) GMT']
    ['★ Unusual Effect: Orbiting Planets', 'This hat adds spice to any occasion.']
    ['Paint Color: Pink as Hell', '★ Unusual Effect: Purple Confetti', "''ABUELO! [8 bit fiesta music] PIEDRAS DE PERDENAL?''"]
    ['★ Unusual Effect: Cloud 9', 'This hat adds spice to any occasion.']
    ['★ Unusual Effect: Orbiting Planets', 'This hat adds spice to any occasion.']
    ['Paint Color: Pink as Hell', "★ Unusual Effect: Nuts n' Bolts", 'This hat adds spice to any occasion.']
    ['★ Unusual Effect: Molten Mallard', 'This hat adds spice to any occasion.']
    

参考

您可以在以下位置找到相关讨论: