第六:selenium鼠标操作和js代码执行

发布时间:2026/6/11 16:25:16
第六:selenium鼠标操作和js代码执行 一.鼠标操作1.鼠标是通过底层接口执行的需要调用ActionChains对象来执行对应的方法2.鼠标操作实现方式2.1.在selenium中将操作鼠标的方法封装在ActionChains类 中实例化对象actionActionChains(driver)2.1.1.context_click(element)右击--模拟鼠标右键点击效果2.1.2.double_click(element)双击--模拟鼠标双击效果2.1.3.drag_and_drop(source,target)拖动--模拟鼠标拖动效果2.1.4.move_to_element(element)悬停--模拟鼠标悬停效果2.1.5.perform()执行--此方法用来执行以上所有鼠标操作2.2.selenium提供鼠标操作的方法及步骤2.2.1.需要导入ActionChains类2.2.2.通过ActionChains实例化鼠标对象 actionActionChains(driver)# driver表示的是浏览器驱动对象2.2.3.调用鼠标的事件方法2.2.4.调用鼠标的执行方法 action.perform()3.clickAndHold单击不释放importtimefromseleniumimportwebdriverfromselenium.webdriver.support.uiimportWebDriverWaitfromselenium.webdriver.supportimportexpected_conditionsasEC ​withwebdriver.Chrome(executable_path./chromedriver)asdriver:# 打开本地文件中的html文件driver.get(file:///Users/superfan/工作/myproject/study/po/action.html)​# click_and_hold 点击且不松开divdriver.find_element_by_xpath(//div[onmousedownmDown(this)])webdriver.ActionChains(driver).click_and_hold(div).perform()time.sleep(2)4.context_click单击importtimefromseleniumimportwebdriverfromselenium.webdriverimportActionChains drwebdriver.Chrome()urlrhttp://www.baidu.comdr.get(url)iptdr.find_element_by_id(kw)actionActionChains(dr)action.context_click(ipt)# 在输入框中右键操作action.perform()time.sleep(3)dr.quit()5.double_click双击importtimefromseleniumimportwebdriverfromselenium.webdriver.support.uiimportWebDriverWaitfromselenium.webdriver.supportimportexpected_conditionsasEC ​withwebdriver.Chrome(executable_path./chromedriver)asdriver:# 打开本地文件中的html文件driver.get(file:///Users/superfan/工作/myproject/study/po/action.html)​# double_click 双击buttondriver.find_element_by_xpath(//button[ondblclick])webdriver.ActionChains(driver).double_click(button).perform()time.sleep(2)6.drag_and_drop拖动6.1.此方法首先在源元素上单击并按住然后移动到目标元素的位置后释放鼠标6.2.调用鼠标拖动事件方法 action.drag_and_drop(source,target)# source表示的是源元素被拖动的元素 target表示是目标源也就是要拖动到哪个元素上importtimefromseleniumimportwebdriverfromselenium.webdriver.support.uiimportWebDriverWaitfromselenium.webdriver.supportimportexpected_conditionsasEC ​withwebdriver.Chrome(executable_path./chromedriver)asdriver:# 打开本地文件中的html文件driver.get(file:///Users/superfan/工作/myproject/study/po/action.html)div1driver.find_element_by_id(draggable)div2driver.find_element_by_id(droppable)webdriver.ActionChains(driver).drag_and_drop(div1,div2).perform()time.sleep(3)7.move_to_element悬停7.1.此方法将鼠标移到元素的中间执行此操作时该元素也会滚动到视图中悬停importtimefromseleniumimportwebdriverfromselenium.webdriver.support.uiimportWebDriverWaitfromselenium.webdriver.supportimportexpected_conditionsasEC ​withwebdriver.Chrome(executable_path./chromedriver)asdriver:# 打开本地文件中的html文件driver.get(file:///Users/superfan/工作/myproject/study/po/action.html)​# move_to_elementdivWebDriverWait(driver,timeout3).until(EC.visibility_of_element_located((xpath,//div[onmouseovermOver(this)])))# 移动到# actionwebdriver.ActionChains(driver)# action.move_to_element(div).perform()webdriver.ActionChains(driver).move_to_element(div).perform()time.sleep(2)8.drag_and_drop_by_offset单元素拖动8.1.action.drag_and_drop_by_offset(element,x,y)x,y 表示的元素拖动时横向和纵向移动的距离单位为像素8.2.element表示的是元素对象 移动的像素最终要比在web页面中看到的移动像素值要大最好大于5个像素或者10像素importtimefromseleniumimportwebdriverfromselenium.webdriverimportActionChains drwebdriver.Chrome()urlrfile:///C:/Users/tang/Desktop/pagetest/%E9%AA%8C%E8%AF%81%E7%A0%81/index.htmldr.get(url)hdr.find_element_by_css_selector(.handler)actionActionChains(dr)action.drag_and_drop_by_offset(h,260,0)# 单元素拖拽 滑动验证action.perform()time.sleep(5)dr.quit()9.moveByOffset9.1.此方法将鼠标从其当前位置(或0,0)移动给定的偏移量如果坐标在视图窗口之外,则鼠标最终将在浏览器窗口之外importtimefromseleniumimportwebdriverfromselenium.webdriver.support.uiimportWebDriverWaitfromselenium.webdriver.supportimportexpected_conditionsasEC ​withwebdriver.Chrome(executable_path./chromedriver)asdriver:# 打开本地文件中的html文件driver.get(file:///Users/superfan/工作/myproject/study/po/action.html)​# 移开 move_by_offsetwebdriver.ActionChains(driver).move_by_offset(xoffset500,yoffset500).perform()time.sleep(2)10.release释放10.1.此操作将释放按下的鼠标左键如果WebElement转移了它将释放给定WebElement上按下的鼠标左键importtimefromseleniumimportwebdriverfromselenium.webdriver.support.uiimportWebDriverWaitfromselenium.webdriver.supportimportexpected_conditionsasEC ​withwebdriver.Chrome(executable_path./chromedriver)asdriver:# 打开本地文件中的html文件driver.get(file:///Users/superfan/工作/myproject/study/po/action.html)# release 松开鼠标webdriver.ActionChains(driver).release(div).perform()time.sleep(2)11.鼠标链式操作fromseleniumimportwebdriverfromselenium.webdriver.support.uiimportWebDriverWaitfromselenium.webdriver.supportimportexpected_conditionsasEC ​withwebdriver.Chrome()asdriver:driver.get(file:///Users/superfan/工作/myproject/study/po/action.html)​# 创建一个动作链actionwebdriver.ActionChains(driver)​# move_to_elementdiv1WebDriverWait(driver,timeout3).until(EC.visibility_of_element_located((xpath,//div[onmouseovermOver(this)])))# 指定等待时间为2saction.move_to_element(div1).pause(2)​# 移开 move_by_offsetaction.move_by_offset(xoffset500,yoffset500).pause(2)​# click_and_hold 点击且不松开div2driver.find_element_by_xpath(//div[onmousedownmDown(this)])action.click_and_hold(div2).pause(2)​# release 松开鼠标action.release(div2).pause(2)​# 执行动作链action.perform()​# 以上代码可重合在一起执行action.move_to_element(div1).pause(2).move_by_offset(xoffset500,yoffset500).pause(2).click_and_hold(div2).pause(2).release(div2).pause(2).perform()二.执行js代码1.selenium执行js有几个方法通常使用 execute_script方法2.页面滚动指定距离importtimefromseleniumimportwebdriver ​withwebdriver.Chrome()asdriver:driver.get(https://image.baidu.com/search/index?tnbaiduimagect201326592lm-1cl2iegb18030word%CD%BC%C6%ACfralaala1alatpladresspos0hs2xthttps111111)# 滚动100px 100表示滚动条距离页面顶部100像素 0是x方向100是y方向driver.execute_script(window.scrollTo(0,100))time.sleep(1)# 200表示滚动条距离页面顶部200像素driver.execute_script(window.scrollTo(0,200))time.sleep(1)driver.execute_script(window.scrollTo(0,300))time.sleep(3)# 移动到底部driver.execute_script(window.scrollTo(0,document.body.scrollHeight))time.sleep(3)# 移动到顶部driver.execute_script(window.scrollTo(0,0))time.sleep(3)3.页面滚动至指定位置3.1.执行js时可以传递参数给js脚本3.2.案例打开页面滚动到指定的元素可见importtimefromseleniumimportwebdriver ​withwebdriver.Chrome(executable_path./chromedriver)asdriver:driver.get(file:///Users/superfan/工作/myproject/study/po/scroll.html)time.sleep(2)divdriver.find_element_by_xpath(//div)# 移动到元素的底端与当前窗口的底部对齐driver.execute_script(arguments[0].scrollIntoView(false);,div)# arguments[0]表示传入第一个值divarguments[1]表示传入第一个值div2# driver.execute_script(arguments[0].scrollIntoView(false);alert(arguments[1]);, div,1)time.sleep(2)# 移动到元素的顶端与当前窗口的顶端对齐driver.execute_script(arguments[0].scrollIntoView();,div)time.sleep(2)