【java+selenium3】Actions模擬鼠標 (十一)


一、鼠標操作

  WebElement的click()方法可實現元素的點擊操作,但是沒有提供鼠標的右擊/雙擊/懸停/鼠標拖動等操作.這些操作需要通過Action類提供的方法來實現!

Action常用的api如下:

1. contextClick() 右擊

2. clickAndHold() 鼠標懸停

3. move_to_element() 鼠標懸停

4. doubleClick() 雙擊

5. dragAndDrop() 拖動

6. release() 釋放鼠標

7. perform() 執行所有Action中的存儲行為

例如:演示模擬驗證碼點擊拖動場景示例如下:

二、代碼准備

 1.前端HTML代碼如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>滑動條</title>
    <link rel="stylesheet" href="drag.css">
    <script src="jquery-1.7.1.min.js"></script>
    <script src="drag.js"></script>
    <style type="text/css">
        .slidetounlock{
            font-size: 12px;
            background:-webkit-gradient(linear,left top,right top,color-stop(0,#4d4d4d),color-stop(.4,#4d4d4d),color-stop(.5,#fff),color-stop(.6,#4d4d4d),color-stop(1,#4d4d4d));
            -webkit-background-clip:text;
            -webkit-text-fill-color:transparent;
            -webkit-animation:slidetounlock 3s infinite;
            -webkit-text-size-adjust:none
        }
        @-webkit-keyframes slidetounlock{0%{background-position:-200px 0} 100%{background-position:200px 0}}

    </style>
</head>
<body>
<div id="wrapper" style="position: relative;top: 300px;left:300px;">
    <div id="drag">
        <div class="drag_bg"></div>
        <div class="drag_text slidetounlock" onselectstart="return false;" unselectable="on">
            請按住滑塊,拖動到最右邊
        </div>
        <div class="handler handler_bg"></div>
    </div>
</div>

    <!--<a href="#" class="img"><img src="img/Lighthouse.jpg"/></a>-->
<script>
    $('#drag').drag();
</script>
</body>
</html>

2.HTML滑塊CSS樣式

#drag{
    position: relative;
    background-color: #e8e8e8;
    width: 300px;
    height: 34px;
    line-height: 34px;
    text-align: center;
}
#drag .handler{
    position: absolute;
    top: 0px;
    left: 0px;
    width: 40px;
    height: 32px;
    border: 1px solid #ccc;
    cursor: move;
}
.handler_bg{
    background: #fff url("../img/slider.png") no-repeat center;
}
.handler_ok_bg{
    background: #fff url("../img/complet.png") no-repeat center;
}
#drag .drag_bg{
    background-color: #7ac23c;
    height: 34px;
    width: 0px;
}
#drag .drag_text{
    position: absolute;
    top: 0px;
    width: 300px;
    color:#9c9c9c;
    -moz-user-select: none;
    -webkit-user-select: none;
    user-select: none;
    -o-user-select:none;
    -ms-user-select:none;

    font-size: 12px;        //  add
}

3.滑塊拖拽JS

$.fn.drag = function(options) {
    var x, drag = this, isMove = false, defaults = {
    };
    var options = $.extend(defaults, options);
    var handler = drag.find('.handler');
    var drag_bg = drag.find('.drag_bg');
    var text = drag.find('.drag_text');
    var maxWidth = drag.width() - handler.width();  //能滑動的最大間距

    //鼠標按下時候的x軸的位置
    handler.mousedown(function(e) {
        isMove = true;
        x = e.pageX - parseInt(handler.css('left'), 10);
    });

    //鼠標指針在上下文移動時,移動距離大於0小於最大間距,滑塊x軸位置等於鼠標移動距離
    $(document).mousemove(function(e) {
        var _x = e.pageX - x;// _x = e.pageX - (e.pageX - parseInt(handler.css('left'), 10)) = x
        if (isMove) {
            if (_x > 0 && _x <= maxWidth) {
                handler.css({'left': _x});
                drag_bg.css({'width': _x});
            } else if (_x > maxWidth) {  //鼠標指針移動距離達到最大時清空事件
                dragOk();
            }
        }
    }).mouseup(function(e) {
        isMove = false;
        var _x = e.pageX - x;
        if (_x < maxWidth) { //鼠標松開時,如果沒有達到最大距離位置,滑塊就返回初始位置
            handler.css({'left': 0});
            drag_bg.css({'width': 0});
        }
    });

    //清空事件
    function dragOk() {
        handler.removeClass('handler_bg').addClass('handler_ok_bg');
        text.removeClass('slidetounlock').text('驗證通過').css({'color':'#fff'});       //modify
       // drag.css({'color': '#fff !important'});

        handler.css({'left': maxWidth});                   // add
        drag_bg.css({'width': maxWidth});                  // add

        handler.unbind('mousedown');
        $(document).unbind('mousemove');
        $(document).unbind('mouseup');

    }
};

滑塊拖動實現代碼參考博文:https://blog.csdn.net/shuai_wy/article/details/62419257

4.jquery-1.7.1.min.js下載鏈接:http://www.jqueryfuns.com/resource/2169

三、自動化代碼實現

@Test
public void test() throws InterruptedException {
    try {
        driver.get("file:///C:/Users/Administrator/Desktop/test/MouseDrag/identifying_code.html");
        driver.manage().window().maximize();
        Actions actions = new Actions(driver);
        WebElement targetElement = driver.findElement(By.xpath("//*[@id=\"drag\"]/div[3]"));
        int x = targetElement.getLocation().getX();
        int y = targetElement.getLocation().getY();
        Thread.sleep(3000);
        //首先定位到方塊並點擊=》移動到目標位置=》松開釋放鼠標=》perform執行Actions的一系列方法調用
        actions.clickAndHold(targetElement).moveToElement(targetElement, x+260, y).release().perform();
        Thread.sleep(3000);
    }catch (Exception e) {
        e.printStackTrace();
    }
}

學習后總結,不足之處后續補充!

未完待續。。。


免責聲明!

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



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