Selenium WebDriver- actionchians模擬鼠標懸停操作


#encoding=utf-8
import unittest
import time
from selenium import webdriver
from selenium.webdriver import ActionChains

class VisitSogouByIE(unittest.TestCase):

    def setUp(self):
        #啟動IE瀏覽器
        #self.driver = webdriver.Firefox(executable_path = "e:\\geckodriver")
        self.driver = webdriver.Ie(executable_path = "e:\\IEDriverServer")
        
    def test_roverOnElement(self):
        url = "http://127.0.0.1/test_mouse_hover.html"
        # 訪問自定義的html網頁
        self.driver.get(url)
        # 找到頁面上第一個鏈接元素
        link1 = self.driver.find_element_by_partial_link_text(u"指過來1")
        # 找到頁面上第二個鏈接元素
        link2 = self.driver.find_element_by_partial_link_text(u"指過來2")
        # 找到頁面上的p元素
        p = self.driver.find_element_by_xpath("//p")
        print link1.text, link2.text
        # 導入需要的Python包
        from selenium.webdriver import ActionChains
        import time
        # 將鼠標懸浮到第一個鏈接元素上
        ActionChains(self.driver).move_to_element(link1).perform()
        time.sleep(2)
        # 將鼠標從第一個鏈接元素移動到p元素上
        ActionChains(self.driver).move_to_element(p).perform()
        time.sleep(2)
        # 將鼠標懸浮到第二個鏈接元素上
        ActionChains(self.driver).move_to_element(link1).perform()
        time.sleep(2)
        # 將鼠標從第二個鏈接元素移動到p元素上
        ActionChains(self.driver).move_to_element(p).perform()
        time.sleep(2)


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

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

 


免責聲明!

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



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