Selenium:如何获得悬停在元素上的元素? [英] Selenium: How to get element of hover over element?

查看:60
本文介绍了Selenium:如何获得悬停在元素上的元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在 shopee 上海网站上抓取商品的交货日期,但我意识到只有当我将鼠标悬停在运费"上时才会出现该元素.在每个项目上.

我设法在检查模式期间冻结调试器中的 javascript,发现交付日期元素的 xpath 是 '//div[@class="sYwpBA"]'

所以到目前为止我所做的是使用 Selenium 的 ActionChains 将鼠标悬停在运费"上.元素,然后让我的驱动程序等到它找到交货日期元素.

但是,它不起作用并且给我空白,这意味着我的司机仍然无法找到交货日期元素.

示例网址:

I am trying to scrape delivery dates for items on shopee's shanghai website and I realized that the element only appears when I hover over "Shipping Fee" on each of the item.

I managed to freeze the javascript in the debugger during inspection mode and found that the delivery date element's xpath is '//div[@class="sYwpBA"]'

So what I did so far is use Selenium's ActionChains to hover over the "Shipping Fee" element and then make my driver wait until it finds the delivery date element.

However, it's not working and giving me blank which means my driver still isn't able to find the delivery date element.

Example url: https://shopee.sg/-Bundle-of-2-SOMEBYMI-150ml-AHA-BHA-PHA-30days-Miracle-Toner-150ml-i.93906301.7143079648?ads_keyword=wkdaelpmissisiht&adsid=3545314&campaignid=1926059&position=0

from selenium import webdriver
from selenium.common import exceptions
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import StaleElementReferenceException
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By


chromedriver = "path to chromedriver"
driver = webdriver.Chrome(chromedriver)
url = https://shopee.sg/-Bundle-of-2-SOMEBYMI-150ml-AHA-BHA-PHA-30days-Miracle-Toner-150ml-i.93906301.7143079648?ads_keyword=wkdaelpmissisiht&adsid=3545314&campaignid=1926059&position=0
driver.get(url)
time.sleep(2) 
    try:
        shipping = driver.find_element_by_xpath('//div/div[@class="shopee-drawer "]')
        hov = ActionChains(driver).move_to_element(shipping)
        hov.perform()
        delivery_date = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, '//div[@class="sYwpBA"]')))
    except (NoSuchElementException, StaleElementReferenceException):
        delivery_date = None

# ROW
row = {
      }

if delivery_date is not None:
    row['Delivery Date'] = delivery_date.text
else:
    row['Delivery Date'] = ""

df = pd.DataFrame(rows)
df.to_csv('delivery_date.csv', index=False)

What can I do to find the delivery date element that's only locatable when I hover over the Shipping Fee element?

解决方案

Can you check with the below XPath's 

try:
time.sleep(5)
shipping = driver.find_element_by_xpath("((//*[name()='svg']//*[name()='g'])[10]/*[name()='path'][starts-with(@d, 'm11 2.5c0 .1 0 .2-.1.3l')])")

action = ActionChains(driver)
action.move_to_element(shipping).perform()

#the below shopee- drawer__contents class is getting displayed after we hover the mouse you can check it in the browser dev tool try debug

delivery_date = driver.find_element_by_xpath("//*[@class='shopee- drawer__contents']/div/div/div[2]").get_attribute("innerText")
except (NoSuchElementException, StaleElementReferenceException):
print(delivery_date)

这篇关于Selenium:如何获得悬停在元素上的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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