• Selenium WebDriver-actionchain模拟键盘操作


    #encoding=utf-8
    import unittest
    import time
    import chardet
    from selenium import webdriver
    from selenium.webdriver import ActionChains
    from selenium.webdriver.common.keys import Keys
    
     
    class VisitSogouByIE(unittest.TestCase):
    
        def setUp(self):
            #启动IE浏览器
            #self.driver = webdriver.Firefox(executable_path = "e:\geckodriver")
            #self.driver = webdriver.Ie(executable_path = "e:\IEDriverServer")
            self.driver = webdriver.Chrome(executable_path = "e:\chromedriver")
            #仅chrome和ie11的最新版本生效
        def test_simulationCombinationKeys(self):
            url = "http://www.baidu.com"
            # 访问百度首页
            self.driver.get(url)
            # 将焦点切换到搜索输入框中
            input = self.driver.find_element_by_id("kw")
            input.send_keys(u"光荣之路")
            time.sleep(2)
            ActionChains(self.driver).key_down(Keys.CONTROL).send_keys('a').
            key_up(Keys.CONTROL).perform()
            time.sleep(2)
            ActionChains(self.driver).key_down(Keys.CONTROL).send_keys('x').
            key_up(Keys.CONTROL).perform()
            self.driver.get(url)
            self.driver.find_element_by_id("kw").click()
            # 模拟Ctrl + V组合键,将从剪切板中获取到的内容粘贴到搜索输入框中
            ActionChains(self.driver).key_down(Keys.CONTROL).send_keys('v').
            key_up(Keys.CONTROL).perform()
            # 点击“百度一下”搜索按钮
            self.driver.find_element_by_id('su').click()
            time.sleep(3)
    
    
        def tearDown(self):
            # 退出IE浏览器
            self.driver.quit()
    
    if __name__ == '__main__':
        unittest.main()
  • 相关阅读:
    Python之路【第二十九篇】:面向对象基础
    Python之路【第二十八篇】:生成器与迭代器
    爬虫实战---爬取猫眼电影
    Selenium库详解
    PyQuery库详解
    BeautifulSoup解析库详解
    深浅拷贝的使用场景分析
    关于大数据量下Core Data的数据迁移
    IOS5中的新增方法详解
    自定义UISearchBar
  • 原文地址:https://www.cnblogs.com/qingqing-919/p/8709431.html
Copyright © 2020-2023  润新知