Selenium3.x Python实现鼠标悬停的几种方法

博文章节

  • 博文章节
    • 本机环境介绍
    • 什么是鼠标悬停
    • 通过move_to_element方法实现
    • 通过调用JS方法实现
    • 通过调用move_by_offset方法实现
    • 通过调用move_to_element_with_offset方法实现
    • 拓展延伸

本机环境介绍

编码语言(Python3.6.2)

(py3env) C:\Users\XXX\iCloudDrive\PycharmProjects\SAT>python -V
Python 3.6.2

Selenium版本(3.7.0)

(py3env) C:\Users\XXX\iCloudDrive\PycharmProjects\SAT>pip show selenium
Name: selenium
Version: 3.7.0
Summary: Python bindings for Selenium

什么是“鼠标悬停”

当鼠标光标移到页面某元素上并停留,页面以文字、图片等形式弹出内容,当鼠标移开时,弹出内容消失。如百度首页以下样式:

Selenium3.x Python实现鼠标悬停的几种方法_第1张图片

通过”move_to_element”方法实现

# -*- coding: utf-8 -*-
import time
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

if __name__ == '__main__':
    chrome_driver_path = "C:\\Users/XXX\\iCloudDrive\\PycharmProjects\\SAT\\sat\\resource\\chromedriver.exe"
    driver = webdriver.Chrome(chrome_driver_path)
    driver.get('https://www.baidu.com/')
    driver.maximize_window()
    menu_xpath = "//a[text()='更多产品']"  # 更多产品XPATH
    more_menu = WebDriverWait(driver=driver, timeout=15).until(EC.visibility_of_element_located((By.XPATH, menu_xpath)))
    ActionChains(driver=driver).move_to_element(more_menu).perform()
    time.sleep(3)  # 仅为能达到悬停效果睡眠,可删除
    driver.quit()

通过调用JS方法实现

# -*- coding: utf-8 -*-
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

if __name__ == '__main__':
    chrome_driver_path = "C:\\Users/zhenfeng.liu\\iCloudDrive\\PycharmProjects\\SAT\\sat\\resource\\chromedriver.exe"
    driver = webdriver.Chrome(chrome_driver_path)
    driver.get('https://www.baidu.com/')
    driver.maximize_window()
    js = """ var evObj = document.createEvent('MouseEvents'); evObj.initMouseEvent(\"mouseover\",true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null); arguments[0].dispatchEvent(evObj); """
    menu_xpath = "//a[text()='更多产品']"
    more_menu = WebDriverWait(driver=driver, timeout=15).until(EC.visibility_of_element_located((By.XPATH, menu_xpath)))
    driver.execute_script(js, more_menu)
    time.sleep(3)
    driver.quit()

通过调用”move_by_offset”方法实现

此方法不常用,因为获取偏移量较为困难.

# -*- coding: utf-8 -*-
import time
from selenium import webdriver
from selenium.webdriver import ActionChains

if __name__ == '__main__':
    chrome_driver_path = "C:\\Users/XXX\\iCloudDrive\\PycharmProjects\\SAT\\sat\\resource\\chromedriver.exe"
    driver = webdriver.Chrome(chrome_driver_path)
    driver.get('https://www.baidu.com/')
    driver.maximize_window()
    ActionChains(driver=driver).move_by_offset(xoffset=1, yoffset=1).perform() # 相对当前位置的X/Y偏移量
    time.sleep(3)
    driver.quit()

通过调用”move_to_element_with_offset”方法实现

此方法不常用,因为获取偏移量较为困难.

# -*- coding: utf-8 -*-
import time
from selenium import webdriver
from selenium.webdriver import ActionChains

if __name__ == '__main__':
    chrome_driver_path = "C:\\Users/zhenfeng.liu\\iCloudDrive\\PycharmProjects\\SAT\\sat\\resource\\chromedriver.exe"
    driver = webdriver.Chrome(chrome_driver_path)
    driver.get('https://www.baidu.com/')
    driver.maximize_window()
    element = driver.find_element_by_name('tj_trnews')
    # 相比参照元素element的X/Y偏移量
    ActionChains(driver=driver).move_to_element_with_offset(to_element=element, xoffset=1, yoffset=1).perform()
    time.sleep(3)
    driver.quit()

拓展延伸

引用:JavaScript获取DOM元素位置和尺寸大小

你可能感兴趣的:(自动化测试)