首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >硒:如何获得悬停在元素上的元素?

硒:如何获得悬停在元素上的元素?
EN

Stack Overflow用户
提问于 2021-07-19 10:51:22
回答 1查看 130关注 0票数 1

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

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

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

然而,它不工作,给我空白,这意味着我的驱动程序仍然无法找到交付日期元素。

示例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

代码语言:javascript
复制
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)

我如何才能找到只有当我将鼠标悬停在Shipping Fee元素上时才能找到的交付日期元素?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-07-19 14:44:13

代码语言:javascript
复制
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)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68434620

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档