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

被刻印的时光 ゝ 提交于 2021-02-19 17:47:41

问题


I am currently trying to scrape a dynamic website which uses javascript to provide information after hovering over images. I am trying to obtain the data inside of the text containers brought up by hovering over these images, but am having difficulty doing so because when I define all of the elements, then attempt to create a loop which hovers over all of them, I only receive the text data from the first element I defined, over and over as many elements as there are (10 on this page.) Here is some code which should help you reproduce my problem. Do I need to induce waits into this loop in order to generate the proper results? Thanks.

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)

The code then proceeds to return exactly what I want for the FIRST element only. Thank you for your time, please let me know if there is an answer to my question somewhere else on the site, I've looked and can't seem to find one.


回答1:


To mouse-hover over multiple elements using Selenium you need to induce WebDriverWait for visibility_of_all_elements_located() and you can use either of the following Locator Strategies:

  • Using 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")))])
    
  • Using 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']")))])
    
  • Note : You have to add the following imports :

    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
    
  • Console Output:

    ['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.']
    

Reference

You can find a relevant discussion in:

  • Scrape website with dynamic mouseover event


来源:https://stackoverflow.com/questions/62376755/how-to-mouse-hover-multiple-elements-using-selenium-and-extract-the-texts-throug

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!