Selenium WebDriver-actionchain模擬鼠標右鍵操作


#encoding=utf-8
import unittest
import time
from selenium import webdriver
from selenium.webdriver import ActionChains
import win32clipboard as w
import win32con

# 設置剪切板內容
def setText(aString):
    w.OpenClipboard()
    w.EmptyClipboard()
    w.SetClipboardData(win32con.CF_UNICODETEXT, aString)
    w.CloseClipboard()


class VisitSogouByIE(unittest.TestCase):

    def setUp(self):
        #啟動IE瀏覽器
        #self.driver = webdriver.Firefox(executable_path = "e:\\geckodriver")
        self.driver = webdriver.Ie(executable_path = "e:\\IEDriverServer")
        #僅能ie生效
        
    def test_rigthClickMouse(self):
        url = "http://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()
        # 將“gloryroad”數據設置到剪切板中,相當於執行了復制操作
        setText(u'gloryroad')
        # 發送一個粘貼命令,字符p指代粘貼操作
        ActionChains(self.driver).send_keys('P').perform()
        # 點擊搜索按鈕
        self.driver.find_element_by_id('stb').click()
        time.sleep(2)



    def tearDown(self):
        # 退出IE瀏覽器
        self.driver.quit()

if __name__ == '__main__':
    unittest.main()

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM