<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    qileilove

    blog已經(jīng)轉(zhuǎn)移至github,大家請訪問 http://qaseven.github.io/

    selenium-webdriver(python) (十五) -- 鼠標(biāo)事件

      本節(jié)重點(diǎn):

      ActionChains 類

         context_click()  右擊

         double_click()   雙擊

         drag_and_drop()  拖動

      測試的產(chǎn)品中有一個操作是右鍵點(diǎn)擊文件列表會彈出一個快捷菜單,可以方便的選擇快捷菜單中的選擇對文件進(jìn)行操作(刪除、移動、重命名),之前學(xué)習(xí)元素的點(diǎn)擊非常簡單:

      driver.find_element_by_id(“xxx”).click()

      那么鼠標(biāo)的雙擊、右擊、拖動等是否也是這樣的寫法呢?例如右擊:

      driver.find_element_by_id(“xxx”).context_click()

      經(jīng)過運(yùn)行腳本得到了下面的錯誤提示:

      AttributeError: 'WebElement' object has no attribute 'context_click'

      提示右點(diǎn)方法不屬于webelement 對象,通過查找文檔,發(fā)現(xiàn)屬于ActionChains 類,但文檔中沒有具體寫法。這里要感謝 北京-QC-rabbit 的指點(diǎn),其實(shí)整個python+selenium 學(xué)習(xí)過程都要感謝 北京-QC-rabbit 的指點(diǎn)。

      下面介紹鼠標(biāo)右鍵的用法,以快播私有云為例:

    #coding=utf-8

    from selenium import webdriver
    from selenium.webdriver.common.action_chains import ActionChains
    import time

    driver = webdriver.Firefox()
    driver.get(http://passport.kuaibo.com/login/?referrer=http%3A%2F%2Fwebcloud.kuaibo.com%2F)

    #登陸快播私有云
    driver.find_element_by_id("user_name").send_keys("username")
    driver.find_element_by_id("user_pwd").send_keys("123456")
    driver.find_element_by_id("dl_an_submit").click()
    time.sleep(3)

    #定位到要右擊的元素
    qqq =driver.find_element_by_xpath("/html/body/div/div[2]/div[2]/div/div[3]/table/tbody/tr/td[2]")
    #對定位到的元素執(zhí)行鼠標(biāo)右鍵操作
    ActionChains(driver).context_click(qqq).perform()


    '''
    #你也可以使用三行的寫法,但我覺得上面兩行寫法更容易理解
    chain = ActionChains(driver)
    implement = driver.find_element_by_xpath("/html/body/div/div[2]/div[2]/div/div[3]/table/tbody/tr/td[2]")
    chain.context_click(implement).perform()
    '''

    time.sleep(3) #休眠3秒
    driver.close()


    這里需要注意的是,在使用ActionChains 類之前,要先將包引入。

      右擊的操作會了,下面的其它方法比葫蘆畫瓢也能寫出來。

      鼠標(biāo)雙擊的寫法:

      #定位到要雙擊的元素

      qqq =driver.find_element_by_xpath("xxx")

      #對定位到的元素執(zhí)行鼠標(biāo)雙擊操作

      ActionChains(driver).double_click(qqq).perform()

      鼠標(biāo)拖放操作的寫法:

    #定位元素的原位置
    element = driver.find_element_by_name("source")
    #定位元素要移動到的目標(biāo)位置
    target =  driver.find_element_by_name("target")

    #執(zhí)行元素的移動操作
    ActionChains(driver).drag_and_drop(element, target).perform()

     ActionChains 類不僅僅是只包含了上面的三個方法,下面將方法列出:

      class ActionChains(driver)

    driver:The WebDriver instance which performs user actions.
    Generate user actions. All actions are stored in the ActionChains object. Call perform() to fire stored actions.
    – perform()
    Performs all stored actions.
    – click(on_element=None)
    Clicks an element.
    on_element:The element to click. If None, clicks on current mouse position.
    – click_and_hold(on_element)
    Holds down the left mouse button on an element.
    on_element:The element to mouse down. If None, clicks on current mouse position.
    – context_click(on_element)
    Performs a context-click (right click) on an element.
    on_element:The element to context-click. If None, clicks on current mouse position.
    – double_click(on_element)
    Double-clicks an element.
    on_element:The element to double-click. If None, clicks on current mouse position.
    – drag_and_drop(source, target)
    Holds down the left mouse button on the source element, then moves to the target element and releases the mouse button.
    source:The element to mouse down.
    target: The element to mouse up.
    – key_down(key, element=None)
    Sends a key press only, without releasing it. Should only be used with modifier keys (Control, Alt andShift).
    key:The modifier key to send. Values are defined in Keys class.
    element:The element to send keys. If None, sends a key to current focused element.
    – key_up(key, element=None)
    Releases a modifier key.
    key:The modifier key to send. Values are defined in Keys class.
    element:The element to send keys. If None, sends a key to current focused element.
    – move_by_offset(xoffset, yoffset)
    Moving the mouse to an offset from current mouse position.
    xoffset:X offset to move to.yoffset:Y offset to move to.
    – move_to_element(to_element)
    Moving the mouse to the middle of an element.
    to_element: The element to move to.
    – move_to_element_with_offset(to_element, xoffset, yoffset)
    Move the mouse by an offset of the specificed element. Offsets are relative to the top-left corner of the
    element.
    to_element: The element to move to.xoffset:X offset to move to.yoffset:Y offset to move to.
    – release(on_element)
    Releasing a held mouse button.
    on_element:The element to mouse up.
    – send_keys(*keys_to_send)
    Sends keys to current focused element.
    keys_to_send:The keys to send.
    – send_keys_to_element(self, element,*keys_to_send):
    Sends keys to an element.
    element:The element to send keys.keys_to_send:The keys to send.

    相關(guān)文章:

    selenium-webdriver(python) (十四) -- webdriver原理

    posted on 2013-09-16 10:28 順其自然EVO 閱讀(563) 評論(0)  編輯  收藏 所屬分類: selenium and watir webdrivers 自動化測試學(xué)習(xí)

    <2013年9月>
    25262728293031
    1234567
    891011121314
    15161718192021
    22232425262728
    293012345

    導(dǎo)航

    統(tǒng)計

    常用鏈接

    留言簿(55)

    隨筆分類

    隨筆檔案

    文章分類

    文章檔案

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 最新国产成人亚洲精品影院| 一级毛片免费全部播放| 色吊丝最新永久免费观看网站| 麻豆91免费视频| 久久亚洲国产成人精品性色| 成人免费视频小说| 免费看无码特级毛片| 亚洲AV无码专区在线亚| 亚洲人成色7777在线观看不卡| 色猫咪免费人成网站在线观看| 精品亚洲国产成人av| 亚洲最新视频在线观看| 国产无遮挡吃胸膜奶免费看视频| 97无码人妻福利免费公开在线视频| 国产精品亚洲一区二区麻豆| 国产精品亚洲一区二区三区在线| 免费无码AV电影在线观看| a级毛片毛片免费观看永久| 国产成人亚洲综合一区| 亚洲AV无码乱码在线观看裸奔| 香蕉高清免费永久在线视频| 精品免费tv久久久久久久| 亚洲AV无码资源在线观看| 亚洲酒色1314狠狠做| 国产av无码专区亚洲国产精品| 无限动漫网在线观看免费| 免费视频一区二区| 无遮挡a级毛片免费看| 亚洲一区中文字幕在线观看| 亚洲精品V欧洲精品V日韩精品| 四虎影在线永久免费四虎地址8848aa| 亚洲电影免费观看| 国产一区二区三区免费观看在线| 国产成人高清亚洲一区91| 亚洲一区在线免费观看| 亚洲视频一区调教| 在线亚洲精品自拍| 免费在线观看中文字幕| 成人au免费视频影院| 亚洲国产精品免费观看| 免费福利电影在线观看|