selenium move_to_element 并不总是鼠标悬停
selenium move_to_element does not always mouse-hover
我正在使用 python 2.7。在尝试将鼠标悬停在菜单项上时,selenium 不会在 Chrome 中始终将鼠标移动到该项目。因此,在单击子菜单时,它最终会单击其他内容。然而,相同的代码在 Firefox 驱动程序中抛出异常。
我读了一些关于 SO 的帖子,这表明 selenium 有时可能很古怪。但是我不知道是不是我做错了什么。
代码如下:
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()
您需要逐步执行此操作检查元素的可见性 您将使用 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()
我正在使用 python 2.7。在尝试将鼠标悬停在菜单项上时,selenium 不会在 Chrome 中始终将鼠标移动到该项目。因此,在单击子菜单时,它最终会单击其他内容。然而,相同的代码在 Firefox 驱动程序中抛出异常。
我读了一些关于 SO 的帖子,这表明 selenium 有时可能很古怪。但是我不知道是不是我做错了什么。
代码如下:
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()
您需要逐步执行此操作检查元素的可见性 您将使用 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()