from selenium import webdriver from time import sleep from selenium.webdriver import ActionChains, Keys # 鼠标点击 # 单击click,双击double_click,右击context_click #鼠标移动 move_to_element( 目标元素 ) #鼠标滑动 click_and_hold( 目标元素 ).move_by_offset().release() #鼠标拖动元素 drag_and_drop(el1, el12) # 鼠标操作需要导入 ActionChains库 #from selenium.webdriver import ActionChains #鼠标滑动操作 def day1(): wb=webdriver.Chrome() wb.get("https://www.woniuxy.com/") sleep(1) wb.maximize_window() sleep(2) #鼠标滑动到页面最下面 wb.execute_script("window.scrollTo(0, document.body.scrollHeight)") sleep(5) #鼠标滑动到页面最上面 wb.execute_script("window.scrollTo(0, -document.body.scrollHeight)") sleep(5) #鼠标滑动到指定位置 target=wb.find_element("xpath","//*[@id='home']/div[8]/div/div[2]/ul/li[1]") wb.execute_script("arguments[0].scrollIntoView();",target) sleep(20) #鼠标移动操作 #移动鼠标到设置元素上, 使用鼠标点击高级设置 def day2(): wb = webdriver.Chrome() wb.get("https://www.baidu.com/") sleep(1) wb.maximize_window() sleep(2) wb_set=wb.find_element("id","s-usersetting-top") #定位到设置上面 ActionChains(wb).click(wb_set).perform() sleep(1) wb_adv=wb.find_element("xpath","//*[@id='s-user-setting-menu']/div/a[2]/span") #定位到高级搜索上面 ActionChains(wb).click(wb_adv).perform() sleep(5) #鼠标滑动操作, 把页面的块 滑动到页面的右边 def day3(): wb = webdriver.Chrome() wb.get("file:///C:/Users/aoxiang.liu/Desktop/demo3.html") sleep(1) wb.maximize_window() sleep(2) wb_move=wb.find_element("id","move") ActionChains(wb).click_and_hold(wb_move).move_by_offset(1000,0).release().perform() sleep(5) def day4(): wb = webdriver.Chrome() wb.get("file:///C:/Users/aoxiang.liu/Desktop/demo4.html") sleep(1) wb.maximize_window() sleep(1) wb_move = wb.find_element("id", "move") wb_h2=wb.find_element("tag name","h2") ActionChains(wb).drag_and_drop(wb_move,wb_h2).release().perform() sleep(2) # 打开百度页面, 使用键盘 操作 输入框 # 输入内容"联想科技有限公司i", # 退格删除i,打2个空格 继续输入内容"python # 全选 ,剪切, 粘贴 , # 继续 全选, 复制 ,清空 ,粘贴 # 按回车键 def day5(): wb = webdriver.Chrome() wb.get("https://www.baidu.com/") sleep(1) wb.maximize_window() sleep(1) # 输入内容"联想科技有限公司i", wb.find_element("id","kw").send_keys("联想科技有限公司i") sleep(1) # 退格删除i,打2个空格 继续输入内容"python wb.find_element("id","kw").send_keys(Keys.BACK_SPACE) sleep(1) wb.find_element("id","kw").send_keys(Keys.SPACE*2) sleep(1) wb.find_element("id","kw").send_keys("python") sleep(1) #全选 ,剪切, 粘贴 wb.find_element("id","kw").send_keys(Keys.CONTROL,"a") sleep(1) wb.find_element("id","kw").send_keys(Keys.CONTROL,"x") sleep(1) wb.find_element("id","kw").send_keys(Keys.CONTROL,"v") sleep(1) #继续 全选, 复制,清空 ,粘贴 wb.find_element("id", "kw").send_keys(Keys.CONTROL, "a") sleep(1) wb.find_element("id", "kw").send_keys(Keys.CONTROL, "c") sleep(1) wb.find_element("id", "kw").clear() sleep(1) wb.find_element("id", "kw").send_keys(Keys.CONTROL, "v") sleep(1) # 按回车键 wb.find_element("id", "kw").send_keys(Keys.ENTER) sleep(2) day5()
文章来源地址https://www.toymoban.com/news/detail-596045.html
文章来源:https://www.toymoban.com/news/detail-596045.html
到了这里,关于python脚本进行web端的鼠标键盘模拟操作的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!