企业网站免费制作,工信和信息化网站备案系统,万网域名备案网站,推广甘肃省5 、 鼠标键盘操作 在浏览器中#xff0c;通常会用到鼠标来进行操作#xff0c;比如右键菜单中选择一个操作#xff0c;在 selenium 中提供了下列鼠标相关操作。 ActionChains 类提供了以下方法#xff1a;
点击鼠标#xff1a;click()右击鼠标#xff1a;context…5 、 鼠标键盘操作 在浏览器中通常会用到鼠标来进行操作比如右键菜单中选择一个操作在 selenium 中提供了下列鼠标相关操作。 ActionChains 类提供了以下方法
点击鼠标click()右击鼠标context_click()双击鼠标double_click()拖拽元素drag_and_drop()长按鼠标click_and_hold()移动鼠标到元素上move_to_element()右击鼠标context_click()模拟键盘上下键滚动send_keys(Keys.PAGE_DOWN) 或 send_keys(Keys.PAGE_UP)模拟鼠标滚轮滚动execute_script(“window.scrollTo(0, document.body.scrollHeight);”)
5.1 鼠标右击 context_click()是Selenium库中的一个方法它可以模拟鼠标右键点击操作。在浏览器中右键点击会弹出一个菜单该菜单显示了可用于该元素的所有操作。context_click()方法可用于执行此操作并选择要执行的操作。 模拟用户在 LMD 登陆界面在输入邮箱地址的输入框右键但是这里本身没有定义右键所以不能打开右键而无法操作右键如果在项目中有用到那右键后的菜单也可以进行定位并操作。示例
from selenium import webdriver
driverwebdriver.Firefox()
driver.get (http://www.chuangyijia.com/admin/login) driver.implicitly_wait(3)
testdriver.find_element_by_id(email)
找到要执行右键操作的元素ActionChains(driver).context_click(test).perform() 对被操作元素执行右键注意事项
context_click()方法需要使用ActionChains类需要先导入该类from selenium.webdriver.common.action_chains import ActionChains在右键点击后选择操作时可以使用send_keys()方法模拟键盘按键操作。在这里我们使用Keys类该类提供了一些常用键的常量例如Keys.CONTROL表示Ctrl键。
5.2 鼠标双击 double_click()是一个鼠标事件函数它会在鼠标双击时被触发。双击事件是指用户快速连续点击鼠标左键两次一般用于实现某些特殊操作或者进行快速编辑等操作。在双击事件中第一次单击会触发单击事件第二次单击会触发双击事件。 在double_click()函数中可以定义一些动作和操作例如打开一个新窗口、删除一条记录、选择一个项等。它主要被用于图形用户界面(GUI)中如各种框架和库中的图形库如Tkinter、Qt、wxWidgets等。因为在GUI中鼠标双击事件是一种非常常见的用户操作。
from selenium import webdriver
driverwebdriver.Firefox()
driver.get (http://www.chuangyijia.com/admin/login) driver.implicitly_wait(3)
testdriver.find_element_by_id(email)
找到要执行右键操ActionChains(driver).double_click(test).perform() 对被操作元素执行双击5.3 鼠标拖放 在一些 web 页面中一些菜单需要将鼠标放上去才会显示它的子菜单在这种情况下自动化需要模拟人为将鼠标放到菜单上。 move_to_element()方法是Selenium中模拟鼠标拖放操作的一种方法。它可以将鼠标指针移动到指定的元素上然后按住鼠标左键拖动该元素到目标位置。 下面通过百度页面来实现这个操作在百度页面中要对搜素的设置进行设置 这种操作需要将鼠标放到页面的设置菜单中才能看到搜索设置才能进行下一步的 操作。代码如下
driver.get(https://www.baidu.com)
打开百度页面setingdriver.find_element_by_link_text(设置)
找到设置ActionChains(driver).move_to_element(seting).perform() 将鼠标移动到设置菜单上
driver.find_element_by_link_text(搜索设置).click() 点击设置下的搜索设置注意事项
move_to_element()方法需要使用ActionChains类需要先导入该类from selenium.webdriver.common.action_chains import ActionChains在移动到源元素和目标元素时可以使用find_element_by_xpath()方法或其他定位方法来定位元素。click_and_hold()方法按下鼠标左键不释放直到使用release()方法释放鼠标左键。
5.4 按键用法 使用键盘时需要导入 selenium.webdriver.common.keys 中的 Keys 模块。 send_keys() 方法是Selenium WebDriver 中用于模拟输入的一种方法。它可以向指定的元素或当前活动的元素发送键盘输入如文本、特殊字符、组合键等。 下面代码模拟用户通过键盘向邮箱地址中输入一个数字。
driver.find_element_by_xpath(//form[idlogin]/input[1]). send_keys(Keys.NUMPAD3)
Keys.NUMPAD3 表示从键盘输入数字 3下面模拟操作 tab 键和 enter 键
driver.find_element_by_xpath(//form[idlogin]/input[1]). send_keys(lib163.com)
输入邮箱driver.find_element_by_xpath(//form[idlogin]/input[2]). send_keys(12345678)
输入密码driver.find_element_by_xpath(//form[idlogin]/input[2]). send_keys(Keys.TAB)
按下 tab 键driver.find_element_by_xpath(//form[idlogin]/button).send_keys(Keys.ENTER)
按下回车键通过上面的代码能够看出输入邮箱和密码之后按下 tab 键操作会切换到登陆按钮上然后在登陆按钮上模拟用户按下 enter 键。 当然这样的操作需要按业务的顺序来的否则会出错。
注意事项
send_keys() 方法需要使用 WebElement 对象需要先通过元素定位方法如 find_element_by_xpath()获取该元素的 WebElement 对象。send_keys() 方法可以一次性输入多个字符使用逗号分隔如 send_keys(“123”, “,”, “abc”) 将输入 “123,abc”。send_keys() 方法还可以模拟各种键盘操作如输入特殊字符、组合键等具体可以参考 Selenium 官方文档或相关教程。
5.5 组合键 在 web 页面使用键盘除了上面的操作之外还可能会有其他操作比如组合键。 接下来通过代码模拟用户在界面输入邮箱地址之后使用 ctrla 的方式将其全选 然后在使用 ctrlc 的方式将内容复制出来登陆成功后将复制的内容粘贴到创意列表的标题中。代码如下
driver.find_element_by_xpath(//form[idlogin]/input[1]). send_keys(lib163.com)
输入邮箱地址driver.find_element_by_xpath(//form[idlogin]/input[1]). send_keys(Keys.CONTROL,a)
将输入的字符串使用 ctrla 键全选driver.find_element_by_xpath(//form[idlogin]/input[1]). send_keys(Keys.CONTROL,c)
在按下 ctrlc将全选的内容复制到剪切板driver.find_element_by_xpath(//form[idlogin]/input[2]). send_keys(12345678)
输入密码driver.find_element_by_xpath(//button[contains(text(),登录 )]).click()
登陆#sleep(2)
driver.implicitly_wait(3)
driver.find_element_by_css_selector(#dashboard-menu li:nth-child(2) a:nth-child(1)).click()
点击待审核管理driver.find_element_by_css_selector(#input01).send_keys(Key s.CONTROL,v)
在搜索栏中的标题输入框中用 ctrlv 粘贴到输入框
文章转载自: http://www.morning.qhrlb.cn.gov.cn.qhrlb.cn http://www.morning.xoaz.cn.gov.cn.xoaz.cn http://www.morning.pcshb.cn.gov.cn.pcshb.cn http://www.morning.blxlf.cn.gov.cn.blxlf.cn http://www.morning.qlckc.cn.gov.cn.qlckc.cn http://www.morning.rzcfg.cn.gov.cn.rzcfg.cn http://www.morning.btgxf.cn.gov.cn.btgxf.cn http://www.morning.xskbr.cn.gov.cn.xskbr.cn http://www.morning.kwqcy.cn.gov.cn.kwqcy.cn http://www.morning.nqbcj.cn.gov.cn.nqbcj.cn http://www.morning.yqkxr.cn.gov.cn.yqkxr.cn http://www.morning.zwndt.cn.gov.cn.zwndt.cn http://www.morning.ksggl.cn.gov.cn.ksggl.cn http://www.morning.jhfkr.cn.gov.cn.jhfkr.cn http://www.morning.xlmgq.cn.gov.cn.xlmgq.cn http://www.morning.ysnbq.cn.gov.cn.ysnbq.cn http://www.morning.nkjnr.cn.gov.cn.nkjnr.cn http://www.morning.thwcg.cn.gov.cn.thwcg.cn http://www.morning.gcxfh.cn.gov.cn.gcxfh.cn http://www.morning.xhpnp.cn.gov.cn.xhpnp.cn http://www.morning.hqwcd.cn.gov.cn.hqwcd.cn http://www.morning.cpzkq.cn.gov.cn.cpzkq.cn http://www.morning.nnwnl.cn.gov.cn.nnwnl.cn http://www.morning.mkrqh.cn.gov.cn.mkrqh.cn http://www.morning.fqyxb.cn.gov.cn.fqyxb.cn http://www.morning.zcxjg.cn.gov.cn.zcxjg.cn http://www.morning.yfcyh.cn.gov.cn.yfcyh.cn http://www.morning.ktrdc.cn.gov.cn.ktrdc.cn http://www.morning.ljygq.cn.gov.cn.ljygq.cn http://www.morning.cklld.cn.gov.cn.cklld.cn http://www.morning.fnzbx.cn.gov.cn.fnzbx.cn http://www.morning.txfxy.cn.gov.cn.txfxy.cn http://www.morning.rlqwz.cn.gov.cn.rlqwz.cn http://www.morning.smdkk.cn.gov.cn.smdkk.cn http://www.morning.lgwjh.cn.gov.cn.lgwjh.cn http://www.morning.lwjlj.cn.gov.cn.lwjlj.cn http://www.morning.mmxnb.cn.gov.cn.mmxnb.cn http://www.morning.bxrlt.cn.gov.cn.bxrlt.cn http://www.morning.wfjrl.cn.gov.cn.wfjrl.cn http://www.morning.pwxkn.cn.gov.cn.pwxkn.cn http://www.morning.dsncg.cn.gov.cn.dsncg.cn http://www.morning.ysnbq.cn.gov.cn.ysnbq.cn http://www.morning.hjrjy.cn.gov.cn.hjrjy.cn http://www.morning.zbmcz.cn.gov.cn.zbmcz.cn http://www.morning.pxlsh.cn.gov.cn.pxlsh.cn http://www.morning.wdlyt.cn.gov.cn.wdlyt.cn http://www.morning.schwr.cn.gov.cn.schwr.cn http://www.morning.jwgmx.cn.gov.cn.jwgmx.cn http://www.morning.rfbq.cn.gov.cn.rfbq.cn http://www.morning.nlwrg.cn.gov.cn.nlwrg.cn http://www.morning.khntd.cn.gov.cn.khntd.cn http://www.morning.nmngg.cn.gov.cn.nmngg.cn http://www.morning.rnqrl.cn.gov.cn.rnqrl.cn http://www.morning.tdxnz.cn.gov.cn.tdxnz.cn http://www.morning.mqgqf.cn.gov.cn.mqgqf.cn http://www.morning.pbsqr.cn.gov.cn.pbsqr.cn http://www.morning.tbkqs.cn.gov.cn.tbkqs.cn http://www.morning.kjnfs.cn.gov.cn.kjnfs.cn http://www.morning.easiuse.com.gov.cn.easiuse.com http://www.morning.mwlxk.cn.gov.cn.mwlxk.cn http://www.morning.hpprx.cn.gov.cn.hpprx.cn http://www.morning.ltdxq.cn.gov.cn.ltdxq.cn http://www.morning.qxlyf.cn.gov.cn.qxlyf.cn http://www.morning.pmdnx.cn.gov.cn.pmdnx.cn http://www.morning.bdtpd.cn.gov.cn.bdtpd.cn http://www.morning.xqjrg.cn.gov.cn.xqjrg.cn http://www.morning.ylqb8.cn.gov.cn.ylqb8.cn http://www.morning.fxkgp.cn.gov.cn.fxkgp.cn http://www.morning.atoinfo.com.gov.cn.atoinfo.com http://www.morning.c7512.cn.gov.cn.c7512.cn http://www.morning.ebpz.cn.gov.cn.ebpz.cn http://www.morning.sdamsm.com.gov.cn.sdamsm.com http://www.morning.kqnwy.cn.gov.cn.kqnwy.cn http://www.morning.rmfwh.cn.gov.cn.rmfwh.cn http://www.morning.psgbk.cn.gov.cn.psgbk.cn http://www.morning.sftrt.cn.gov.cn.sftrt.cn http://www.morning.egmux.cn.gov.cn.egmux.cn http://www.morning.mlpmf.cn.gov.cn.mlpmf.cn http://www.morning.nmwgd.cn.gov.cn.nmwgd.cn http://www.morning.vibwp.cn.gov.cn.vibwp.cn