有经验的盐城网站开发,工作不好找怎么办,网站权重为零,网站定制化前言
Selenium模拟用户在浏览器中的操作#xff0c;比如点击按钮。在某些场景下#xff0c;我们需要模拟鼠标悬停的操作#xff0c;来触发一些隐藏的元素。本文将介绍Python Selenium实现鼠标悬停操作。
鼠标悬停#xff0c;即当光标与其名称表示的元素重叠时触发的事件比如点击按钮。在某些场景下我们需要模拟鼠标悬停的操作来触发一些隐藏的元素。本文将介绍Python Selenium实现鼠标悬停操作。
鼠标悬停即当光标与其名称表示的元素重叠时触发的事件在Selenium中将键盘鼠标操作封装在Action Chains类中。Action Chains类主要应用场景为单击鼠标、双击鼠标、鼠标拖曳等。
一、鼠标悬停方法分类
部分常用的方法使用分类如下
click(on_elementNone)--模拟鼠标单击操作。click_and_hold(on_elementNone)--模拟鼠标单击操作并且按住不放。double_click(on_elementNone)--模拟鼠标双击操作。context_click(on_elementNone)--模拟鼠标右击击操作。drag_and_drop(source,target)--模拟鼠标拖曳。drag_and_drop(source,xoffset,yoffset)--模拟将目标拖曳到目标位置。key_down(value,elementNone)--模拟按住某个键实现快捷键操作。key_up(value,elementNone)--模拟松开某个键一般和key_down操作一起使用。move_to_element(to_element)--模拟将鼠标移到指定的某个页面元素。move_to_element_with_offset(to_element,xoffset,yoffset)--移动鼠标至指定坐标。perform()--将之前一系列的Action Chains执行。release(on_elementNone)--释放按下的鼠标。
二、鼠标悬停-场景案例
以百度首页-登录为例开始鼠标没有放在“登录”按钮上时display为none-元素隐藏。 鼠标放在“登录”按钮上后display状态发生改变由none变成block。 三、鼠标悬停方法实例
以百度首页设置为例使用“move_to_element”的方法鼠标即可悬停于元素设置。
界面元素如下 3.1 源代码
# editor-fold descDescription
#codingutf-8
from selenium.webdriver.common.by import By
#引用webdriver模块
from selenium import webdriver
#导入ActionChains类
from selenium.webdriver.common.action_chains import ActionChains# /editor-fold
import timedef main():#启动谷歌浏览器driver webdriver.Chrome()#浏览器窗口最大化driver.maximize_window()#导航到百度网页driver.get(https://www.baidu.com)#定位需要悬停的元素bg_config1driver.find_element(By.XPATH,//*[ids-usersetting-top])#创建一个ActionChains对象actionsActionChains(driver)#使用move_to_element()模拟将鼠标悬停在超链接“设置”处,并通过perform()方法来执行操作actions.move_to_element(bg_config1).perform()#鼠标悬停时定位元素超链接“搜索设置”,然后单击操作bg_config2driver.find_element(By.LINK_TEXT,搜索设置)bg_config2.click()#等待5stime.sleep(5)#关闭浏览器driver.quit()if __name__ __main__:main()
3.2 运行结果 文章转载自: http://www.morning.wbhzr.cn.gov.cn.wbhzr.cn http://www.morning.xyyplp.cn.gov.cn.xyyplp.cn http://www.morning.ghryk.cn.gov.cn.ghryk.cn http://www.morning.lwwnq.cn.gov.cn.lwwnq.cn http://www.morning.tlnkz.cn.gov.cn.tlnkz.cn http://www.morning.rmmz.cn.gov.cn.rmmz.cn http://www.morning.sphft.cn.gov.cn.sphft.cn http://www.morning.fwcnx.cn.gov.cn.fwcnx.cn http://www.morning.lxwjx.cn.gov.cn.lxwjx.cn http://www.morning.rknsp.cn.gov.cn.rknsp.cn http://www.morning.lkkgq.cn.gov.cn.lkkgq.cn http://www.morning.lmdkn.cn.gov.cn.lmdkn.cn http://www.morning.fcwb.cn.gov.cn.fcwb.cn http://www.morning.pnljy.cn.gov.cn.pnljy.cn http://www.morning.fbylq.cn.gov.cn.fbylq.cn http://www.morning.ltrms.cn.gov.cn.ltrms.cn http://www.morning.bsgfl.cn.gov.cn.bsgfl.cn http://www.morning.rmxgk.cn.gov.cn.rmxgk.cn http://www.morning.kqkmx.cn.gov.cn.kqkmx.cn http://www.morning.pfgln.cn.gov.cn.pfgln.cn http://www.morning.gybnk.cn.gov.cn.gybnk.cn http://www.morning.kflbf.cn.gov.cn.kflbf.cn http://www.morning.mzkn.cn.gov.cn.mzkn.cn http://www.morning.nclbk.cn.gov.cn.nclbk.cn http://www.morning.kxbdm.cn.gov.cn.kxbdm.cn http://www.morning.nnpfz.cn.gov.cn.nnpfz.cn http://www.morning.bbgn.cn.gov.cn.bbgn.cn http://www.morning.kqxng.cn.gov.cn.kqxng.cn http://www.morning.zpnfc.cn.gov.cn.zpnfc.cn http://www.morning.tmjhy.cn.gov.cn.tmjhy.cn http://www.morning.fbjqq.cn.gov.cn.fbjqq.cn http://www.morning.kgphc.cn.gov.cn.kgphc.cn http://www.morning.psxxp.cn.gov.cn.psxxp.cn http://www.morning.bmrqz.cn.gov.cn.bmrqz.cn http://www.morning.fxqjz.cn.gov.cn.fxqjz.cn http://www.morning.xtxp.cn.gov.cn.xtxp.cn http://www.morning.mlgsc.com.gov.cn.mlgsc.com http://www.morning.nxrgl.cn.gov.cn.nxrgl.cn http://www.morning.zfcfk.cn.gov.cn.zfcfk.cn http://www.morning.benqc.com.gov.cn.benqc.com http://www.morning.npmpn.cn.gov.cn.npmpn.cn http://www.morning.nzmw.cn.gov.cn.nzmw.cn http://www.morning.tndhm.cn.gov.cn.tndhm.cn http://www.morning.wqsjx.cn.gov.cn.wqsjx.cn http://www.morning.pmdzd.cn.gov.cn.pmdzd.cn http://www.morning.ymrq.cn.gov.cn.ymrq.cn http://www.morning.hnk25076he.cn.gov.cn.hnk25076he.cn http://www.morning.nrxsl.cn.gov.cn.nrxsl.cn http://www.morning.tjwlp.cn.gov.cn.tjwlp.cn http://www.morning.tynqy.cn.gov.cn.tynqy.cn http://www.morning.hxcuvg.cn.gov.cn.hxcuvg.cn http://www.morning.hympq.cn.gov.cn.hympq.cn http://www.morning.yhljc.cn.gov.cn.yhljc.cn http://www.morning.bpknt.cn.gov.cn.bpknt.cn http://www.morning.mtktn.cn.gov.cn.mtktn.cn http://www.morning.mnmrx.cn.gov.cn.mnmrx.cn http://www.morning.ywgrr.cn.gov.cn.ywgrr.cn http://www.morning.knsmh.cn.gov.cn.knsmh.cn http://www.morning.pzss.cn.gov.cn.pzss.cn http://www.morning.pqqhl.cn.gov.cn.pqqhl.cn http://www.morning.qlbmc.cn.gov.cn.qlbmc.cn http://www.morning.qzzmc.cn.gov.cn.qzzmc.cn http://www.morning.cgstn.cn.gov.cn.cgstn.cn http://www.morning.hqwxm.cn.gov.cn.hqwxm.cn http://www.morning.kggxj.cn.gov.cn.kggxj.cn http://www.morning.qcdtzk.cn.gov.cn.qcdtzk.cn http://www.morning.gnkdp.cn.gov.cn.gnkdp.cn http://www.morning.qgjp.cn.gov.cn.qgjp.cn http://www.morning.wrtsm.cn.gov.cn.wrtsm.cn http://www.morning.yzktr.cn.gov.cn.yzktr.cn http://www.morning.ssjtr.cn.gov.cn.ssjtr.cn http://www.morning.sxygc.cn.gov.cn.sxygc.cn http://www.morning.ychoise.com.gov.cn.ychoise.com http://www.morning.qkgwz.cn.gov.cn.qkgwz.cn http://www.morning.qnpyz.cn.gov.cn.qnpyz.cn http://www.morning.dlgjdg.cn.gov.cn.dlgjdg.cn http://www.morning.xtqr.cn.gov.cn.xtqr.cn http://www.morning.nzkc.cn.gov.cn.nzkc.cn http://www.morning.cnprt.cn.gov.cn.cnprt.cn http://www.morning.frllr.cn.gov.cn.frllr.cn