Python selenium move_to_element 并不总是鼠标悬停

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27934945/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 02:29:32  来源:igfitidea点击:

selenium move_to_element does not always mouse-hover

pythongoogle-chromepython-2.7seleniumselenium-webdriver

提问by user3262242

I am using python 2.7. While trying to hover the mouse on a menu item, selenium does not move the mouse to the item consistently in Chrome. Hence while clicking on a sub menu, it ends up clicking something else. However the same code throws exception in Firefox driver.

我正在使用 python 2.7。在尝试将鼠标悬停在菜单项上时,selenium 不会在 Chrome 中始终将鼠标移动到该项目上。因此,在单击子菜单时,它最终会单击其他内容。但是相同的代码在 Firefox 驱动程序中引发异常。

I read few posts on SO which indicates that selenium can be sometimes quirky. But I can't figure out whether I am doing something wrong.

我读了几篇关于 SO 的帖子,这表明硒有时可能很古怪。但我不知道我是否做错了什么。

Here is the code:

这是代码:

from selenium import webdriver
from time import sleep
from selenium.webdriver.common.action_chains import ActionChains

driver = webdriver.Chrome()
#driver = webdriver.Firefox()
driver.get("http://www.flipkart.com/watches/pr?p%5B%5D=facets.ideal_for%255B%255D%3DMen&p%5B%5D=sort%3Dpopularity&sid=r18&facetOrder%5B%5D=ideal_for&otracker=ch_vn_watches_men_nav_catergorylinks_0_AllBrands")
driver.maximize_window()
sleep(10)

elm_Men_Menu = driver.find_element_by_xpath("//li[@class='menu-l0 ']/a[@data-tracking-id='men']")
elm_FastTrack_Menu = driver.find_element_by_xpath("//li[@class='menu-item']/a[@data-tracking- id='0_Fastrack']")

builder = ActionChains(driver)
builder.move_to_element(elm_Men_Menu).click(elm_FastTrack_Menu).perform()

采纳答案by alecxe

You need to do this step by step checking the visibility of the elementsyou are going to interact with using Explicit Waits, do not use time.sleep()- it is not reliable and error-prone:

您需要使用Explicit Waits逐步检查要与之交互的元素的可见性,请勿使用- 它不可靠且容易出错:time.sleep()

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome()

driver.get("http://www.flipkart.com/watches/pr?p%5B%5D=facets.ideal_for%255B%255D%3DMen&p%5B%5D=sort%3Dpopularity&sid=r18&facetOrder%5B%5D=ideal_for&otracker=ch_vn_watches_men_nav_catergorylinks_0_AllBrands")
driver.maximize_window()

# wait for Men menu to appear, then hover it
men_menu = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "//a[@data-tracking-id='men']")))
ActionChains(driver).move_to_element(men_menu).perform()

# wait for Fastrack menu item to appear, then click it
fastrack = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "//a[@data-tracking-id='0_Fastrack']")))
fastrack.click()