编码语言(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
当鼠标光标移到页面某元素上并停留,页面以文字、图片等形式弹出内容,当鼠标移开时,弹出内容消失。如百度首页以下样式:
# -*- 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()
# -*- 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()
此方法不常用,因为获取偏移量较为困难.
# -*- 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()
此方法不常用,因为获取偏移量较为困难.
# -*- 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元素位置和尺寸大小