selenium - 鼠标悬停点击

前言:

常见一些网页都是带有类似下图,当鼠标悬停在隐藏文本内容上时,显示所有内容。例如:我要点击“搜索设置”,先得把鼠标放在‘设置上’

 F12 - 在页面中搜索‘高级搜索’,找到“搜索设置”文本,鼠标放到‘设置’上,display的值变为 block;鼠标不放上去之前是 none,即不可见元素。 

 

隐藏的元素操作,会出现报错:ElementNotInteractableException: Message: element not interactable --- 元素不可交互(强行乱来)

 

一、处理方案:

Selenium 提供了 ActionChains 模块来处理鼠标操作。

鼠标的操作有:悬停双击右击等。

move_to_element()  #移动到元素上
double_click()      #双击
context_click():   #右击
perform():        #执行前面的动作

 

二、实操:

就以百度为例

# coding:utf-8

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from time import sleep


driver  = webdriver.Chrome()    #驱动
driver.get('https://www.baidu.com')
sleep(1)
driver.maximize_window()    #最大窗口化
sleep(1)
set_up=driver.find_element_by_xpath('//*[@id="s-usersetting-top"]')
ActionChains(driver).move_to_element(set_up).perform()
sleep(1)
advanced_search=driver.find_element_by_xpath('//*[@id="s-user-setting-menu"]/div/a[1]/span')
advanced_search.click()

 

 

再看一下某社区的例子:

 

 

 

tips:如果Chrome老是自动更新版本,你的驱动版本也得更新

 

posted @ 2023-08-29 11:34  小林同学_Scorpio  阅读(419)  评论(0编辑  收藏  举报
1