如何使用Selenium将鼠标悬停在多个元素上并通过Python提取文本 [英] How to mouse hover multiple elements using Selenium and extract the texts through Python

查看:300
本文介绍了如何使用Selenium将鼠标悬停在多个元素上并通过Python提取文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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)

然后,代码继续进行操作,以完全返回我只对FIRST元素想要的内容.谢谢您的宝贵时间,请让我知道网站上其他地方是否存在我的问题的答案,我已经查看了,但似乎找不到.

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.

推荐答案

使用 WebDriverWait 用于 visibility_of_all_elements_located(),您可以使用以下

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:

  • 使用 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']")))])
    

  • 注意:您必须添加以下导入:

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

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

    You can find a relevant discussion in:

    这篇关于如何使用Selenium将鼠标悬停在多个元素上并通过Python提取文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆