【问题标题】:Selenium: How to get element of hover over element?Selenium:如何让元素悬停在元素上?
【发布时间】:2021-07-19 02:51:21
【问题描述】:

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

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

所以到目前为止,我所做的是使用 Selenium 的 ActionChains 将鼠标悬停在“运费”元素上,然后让我的司机等到它找到交货日期元素。

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

示例网址: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)

如何才能找到只有在我将鼠标悬停在运费元素上时才能找到的交货日期元素?

【问题讨论】:

    标签: python selenium selenium-webdriver web-scraping xpath


    【解决方案1】:
    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)
    

    【讨论】:

    • 不确定您希望我将代码放在哪里?我对为什么在下一行中定义之前在 action.move 中使用元素感到困惑,我似乎找不到任何以“shopee-drawer__contents”作为其类的元素
    • 您能否更新代码以使其清晰?另外,我发现 svg 和标签的 xpath 是 //div[@class="flex items-center deQMhv"]//*[name()='svg']//*[name()='g ']//*[name()='path'] 准确地说。我发现页面加载时会显示更多元素,因此 xpath 中的“[10]”不起作用
    • shopee-drawer__contents 当您将鼠标悬停在 SVG 元素上时会显示此内容,顺便说一句,我的 XPath 不显示更多元素它正在显示完全需要的元素我不知道您如何检查它跨度>
    猜你喜欢
    • 1970-01-01
    • 2014-01-15
    • 2011-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-05
    • 2011-11-08
    相关资源
    最近更新 更多