from selenium import webdriver import time from selenium.webdriver import ActionChains
driver = webdriver.Chrome() driver.get(“https://www.baidu.cn”)
#定位到需要右击的元素,然后执行鼠标右击操作(例:对新闻标签进行右击) context_click_location = driver.find_element_by_xpath(’//*[@id=“hotsearch-content-wrapper”]/li[1]/a/span[2]’) ActionChains(driver).click(context_click_location).perform()
time.sleep(2) #睡两秒,看一下效果
定位到需要悬停的元素,然后执行鼠标悬停操作(例:对设置标签进行悬停)
move_to_element_location = driver.find_element_by_xpath("/html/body/div[1]/div[1]/div/div[3]/a[8]")
ActionChains(driver).move_to_element(move_to_element_location).perform()
driver.fin
time.sleep(2) #睡两秒,看一下效果
# 鼠标悬浮后点击高级搜索
driver.find_element_by_xpath("/html/body/div[1]/div[6]/a[2]").click()
time.sleep(2) #睡两秒,看一下效果
driver.quit() #关闭所有标签页
|