鼠标在python selenium中操作

时间:2017-01-06 09:28:50

标签: python selenium

我正尝试在python selenium绑定中执行鼠标悬停操作,但在使用ActionChains.perform()

时出错

我试过这个

def test_mouse_over():

    driver =webdriver.Firefox()
    driver.get("https://www.flipkart.com/")

    actions = ActionChains(driver)
    val1 = driver.find_element_by_xpath('//span[text()="Men"]')
    actions.move_to_element(val1)
    val2 = driver.find_element_by_xpath('//span[text()="Shirts"]')
    actions.click(val2)
    actions.perform()

我在最后一行actions.perform()

收到错误

2 个答案:

答案 0 :(得分:1)

您无需在链中执行这两个操作。请尝试以下代码:

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

driver = webdriver.Firefox()
driver.get("https://www.flipkart.com/")

actions =chains(driver)
val1 = driver.find_element_by_xpath('//li[a[@title="Men"]]')
actions.move_to_element(val1).perform()
val2 = wait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//a[span[text()="Shirts"]]')))
val2.click()

答案 1 :(得分:0)

正确的解决方案

def test_mouse_over():
    """mouse over operation FLIPKART.COM"""
    driver = webdriver.Chrome("C:\Python27\Scripts\chromedriver.exe")  # in chrome this code is working

    # driver = webdriver.Firefox() # in forefox this code is not working

    driver.get("https://www.flipkart.com/")
    driver.implicitly_wait(20)
    driver.maximize_window()

    menu = driver.find_element_by_xpath("//span[text()='Women']")
    hidden_submenu = driver.find_element(By.XPATH, "//span[text()='Flats']")

    actions = ActionChains(driver)

    actions.move_to_element(menu).click().perform()

    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Flats']")))
    try:
        actions.click(hidden_submenu).perform()
    except:
        WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Flats']")))