wangyafeng

模拟键盘

 1 from selenium import webdriver
 2 import time
 3 
 4 Browser = webdriver.Firefox()                                                                 #设置浏览器
 5 Browser.get("https://www.baidu.com")                                                          #打开浏览器
 6 \'\'\'
 7 1、点击:                .click()
 8 2、清空:                 .clear()
 9 3、输入字符串:          .send_keys("字符串")
10 4、提交表单              .submit()                           #一般用于模拟Enter回车键盘
11 #如果有些的确不行怎么办
12 \'\'\'
13 \'\'\'
14 from selenium.webdriver.common.keys import Keys             #导入键盘模块      
15 \'\'\'
16 from selenium.webdriver.common.keys import Keys
17 Browser.find_element_by_id("kw").send_keys("id:kw")
18 time.sleep(10)
19 Browser.find_element_by_id("su").send_keys(Keys.ENTER)      #模拟Enter键
20 time.sleep(10)
21 Browser.quit()
22 \'\'\'
23 1、键盘F1--F12:      send_keys(Keys.F1)
24 2、复制Ctrl+C:       send_keys(Keys.CONTROL,\'c\')
25 3、粘贴Ctrl+V:       send_keys(Keys.CONTROL,\'v\')
26 4、全选Ctrl+A:       send_keys(Keys.CONTROL,\'a\')
27 5、剪切Ctrl+X:       send_keys(Keys.CONTROL,\'x\')
28 6、制表键Tab:         send_keys(Keys.TAB)
29 \'\'\'

模拟鼠标

 1 from selenium import webdriver
 2 from selenium.webdriver.common.action_chains import ActionChains
 3 import time
 4 
 5 Browser = webdriver.Firefox()                                                                 #设置浏览器
 6 Browser.get("https://www.baidu.com")                                                          #打开浏览器
 7 
 8 Browser.implicitly_wait(5)                                                                    #等待超时
 9 Browser.set_page_load_timeout(60)
10 \'\'\'
11 
12     driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);      //识别元素时的超时时间
13     driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);  //页面加载时的超时时间
14     driver.manage().timeouts().setScriptTimeout(10, TimeUnit.SECONDS);  //异步脚本的超时时间
15 
16 \'\'\'
17 # #鼠标悬停在搜索设置按钮上
18 # mouse=Browser.find_element_by_link_text("学术")              ins                                 #找到
19 # ActionChains(Browser).move_to_element(mouse).perform()                                        #悬停  字体变色
20 # time.sleep(5)
21 # Browser.quit()
22 
23 \'\'\'
24 右击鼠标:context_click()
25 双击鼠标:double_click()
26 依葫芦画瓢,替换上面案例中对应的鼠标事件就可以了
27 \'\'\'

 

分类:

技术点:

相关文章: