webdriver API(10)模拟按键与鼠标

实现全选、剪切、粘贴及回车的操作

#encoding=utf-8
from selenium import webdriver
import unittest
import win32api
import win32con
import time
VK_CODE={
    'enter':0x0D,
    'ctrl':0x11,
    'a':0x41,
    'v':0x56,
    'x':0x58
    }
def keyDown(keyName):
    win32api.keybd_event(VK_CODE[keyName],0,0,0)
def keyUp(keyName):
    win32api.keybd_event(VK_CODE[keyName],0,win32con.KEYEVENTF_KEYUP,0)

class VisitByFirefox(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox(executable_path="f:\\geckodriver")
    def test_simulationCombinationKeys(self):
        url="https://www.sogou.com"
        self.driver.get(url)
        searchBox=self.driver.find_element_by_id("query")
        searchBox.click()
        searchBox.send_keys("光荣之路自动化测试")
        time.sleep(3)
        keyDown('ctrl')
        keyDown('a')
        keyUp('ctrl')
        keyUp('a')
        keyDown('ctrl')
        keyDown('x')
        keyUp('ctrl')
        keyUp('x')
        self.driver.get("https://www.baidu.com")
        self.driver.find_element_by_id("kw").click()
        keyDown('ctrl')
        keyDown('v')
        keyUp('v')
        keyUp('ctrl')
        keyDown('enter')
        keyUp('enter')
        time.sleep(5)

    def tearDown(self):
        self.driver.quit()
    if __name__ == '__main__':
        unittest.main()

通过设置剪贴板实现复制粘贴

#encoding=utf-8
from selenium import webdriver
import unittest
from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys
import win32clipboard as w
import win32con
import time
def getText():
    w.OpenClipboard()
    d=w.GetClipboardData(win32con.CF_TEXT)
    w.CloseClipboard()
    return d
def setText(aString):
    w.OpenClipboard()
    w.EmptyClipboard()
    w.SetClipboardData(win32con.CF_UNICODETEXT,aString)
    w.CloseClipboard()
class VisitByFirefox(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox(executable_path="f:\\geckodriver")
    def test_copyAndPaste(self):
        url="https://www.baidu.com"
        self.driver.get(url)
        content="光荣之路"
        setText(content)
        getContent=getText()
        print(getContent.decode("gbk").encode("utf-8"))
        self.driver.find_element_by_id("kw").click()
        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):
        self.driver.quit()
if __name__ == '__main__':
        unittest.main()

模拟鼠标右键

#encoding=utf-8
from selenium import webdriver
import unittest
from selenium.webdriver import ActionChains
import win32clipboard as w
import win32con
import time
def setText(aString):
    w.OpenClipboard()
    w.EmptyClipboard()
    w.SetClipboardData(win32con.CF_UNICODETEXT,aString)
    w.CloseClipboard()

class VisitByFirefox(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox(executable_path="f:\\geckodriver")
    def test_rightClickMouse(self):
        url="https://www.sogou.com"
        self.driver.get(url)
        searchBox=self.driver.find_element_by_id("query")
        searchBox.click()
        time.sleep(2)
        ActionChains(self.driver).context_click(searchBox).perform()
        setText('gloryroad')
        ActionChains(self.driver).send_keys('P').perform()
        self.driver.find_element_by_id('stb').click()
        time.sleep(2)
    def tearDown(self):
        self.driver.quit()
if __name__ == '__main__':
        unittest.main()

模拟鼠标左键按下与释放

html代码:

<html>
<meta charset="UTF-8">
<head>
<meta http-equiv="Content-Type"content="text/html;charset="utf-8"/>
<script type="text/javascript">
	function mouseDownFun()
	{
		document.getElementById('div1').innerHTML +='鼠标左键被按下<br/>';
	}
	function mouseUpFun()
	{
		document.getElementById('div1').innerHTML +='已经被按下的鼠标左键被释放抬起<br/>';
		}
	function clickFun()
	{
		document.getElementById('div1').innerHTML +='单击动作发生<br/>';
		}
	
</script>
</head>
<body>
	<div id ="div1"onmousedown="mouseDownFun;"onmouseup="mouseDownFun;"
	onclick="clickFun();"style="background:#CCC;border:3px solid#999;
	width:200px;height:200px;padding:10px">
	</div>
	<input style="margin-top:10px"type="button"
	onclick="document.getElementById('div1').innerHTML='';"value="清除信息"/>
</body>
<html>

实例:

#encoding=utf-8
from selenium import webdriver
import unittest
class VisitByFirefox(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox(executable_path="f:\\geckodriver")
    def test_simulationLefClickMouseProcess(self):
        url="file:///F:/10.html"
        self.driver.get(url)
        div=self.driver.find_element_by_id("div1")
        from selenium.webdriver import ActionChains
        import time
        ActionChains(self.driver).click_and_hold(div).perform()
        time.sleep(2)
        ActionChains(self.driver).release(div).perform()
        time.sleep(2)
        ActionChains(self.driver).click_and_hold(div).perform()
        time.sleep(2)
        ActionChains(self.driver).release(div).perform()
    def tearDown(self):
        self.driver.quit()
if __name__ == '__main__':
        unittest.main()

判断页面元素是否存在

#encoding=utf-8
from selenium import webdriver
import unittest
class VisitByFirefox(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox(executable_path="f:\\geckodriver")
    def isElementPresent(self,by,value):
        from selenium.common.exceptions import NoSuchElementException
        try:
            element=self.driver.find_element(by=by,value=value)
        except NoSuchElementException as e:
            print(e)
            return False
        else:
            return True
    def test_isElementPresent(self):
        url="https://www.sogou.com"
        self.driver.get(url)
        res=self.isElementPresent("id","query")
        if res is True:
            print("所查找的内容存在于页面上!")
        else:
            print("页面未找到所需要的页面元素!")

    def tearDown(self):
        self.driver.quit()
if __name__ == '__main__':
        unittest.main()

保持鼠标悬停在某个元素上
HTML代码:

<html>
<meta http-equiv="Content-Type"content="text/html;charset=utf-8"/>
<head>
	<script language="javascript">
		function showNone()
		{
			document.getElementById('div1').style.display="none";
		}
		function showBlock()
		{
		
			document.getElementById('div1').style.display="block";
	}
	</script>
	<style type="text/css">
		#div1{
			position:absolute;
			width:200px;
			height:115px;
			z-index:1;
			left:28px;
			top:34px;
			background-color:#0033CC;}
	</style>
</head>
<body "showNone()">
	<div id ="div1"></div>
	<a onmouseover ="showBlock()"onmouseout="showNone()"id="link1">鼠标指过来1</a>
	<a onmouseover ="showBlock()"onmouseout="showNone()"id="link2">鼠标指过来2</a>
	<p>鼠标悬浮这里的时候,蓝色的图形框就消失了<p>
</body>
</html>

实例代码:

#encoding=utf-8
from selenium import webdriver
import unittest
class VisitByFirefox(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox(executable_path="f:\\geckodriver")
    def test_roverOnElement(self):
        url="file:///F:/111.html"
        self.driver.get(url)
        link1=self.driver.find_element_by_partial_link_text("指过来1")
        link2=self.driver.find_element_by_partial_link_text("指过来2")
        p=self.driver.find_element_by_xpath("//p")
        print(link1.text,link2.text)
        from selenium.webdriver import ActionChains
        import time
        ActionChains(self.driver).move_to_element(link1).perform()
        time.sleep(2)
        ActionChains(self.driver).move_to_element(p).perform()
        time.sleep(2)
        ActionChains(self.driver).move_to_element(link2).perform()
        time.sleep(2)
        ActionChains(self.driver).move_to_element(p).perform()
        time.sleep(2)
    def tearDown(self):
        self.driver.quit()
if __name__ == '__main__':
        unittest.main()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值