Selenium WebDriver滚动网页

参考地址:https://www.yiibai.com/selenium/selenium-webdriver-scrolling-web-page.html#article-start

测试页面:

<html>

    <head>

        <title>简单测试页面</title>

        <meta charset="utf-8">

          <meta name="viewport" content="width=device-width, initial-scale=1">

          <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

          <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

          <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

        <style></style>

    </head>

    <body style="font-family: cursive;">

        <div class="container">

            <div class="row">

                <div class="col-md-offset-2 col-md-8" style="font-size: 30; margin-top: 40px; ">

                    用于自动化测试的Web页面示例

                </div>

            </div>

            <div class="row">

                <div class="col-md-12" style="font-size:20px; margin-top:40px;">

                    This is sample webpage with dummy elements that will help you in learning selenium automation.

                </div>

            </div>

            <br>

            <div class="row">

                <div class="col-md-12" style="font-size:15px;">

                    <b>This is sample text.</b>

                </div>

            </div>

            <br>

            <div class="row">

                <div class="col-md-12" style="font-size:15px;">

                    <p> <b>Link : </b><a href="https://www.yiibai.com/">This is a link</a></p>

                </div>

            </div>

            <br>

            <div class="row">

                <div class="col-md-12" style="font-size:15px;">

                    <p><b>TextBox : </b><input id="fname" type="text" name="firstName" ></p>

                </div>

            </div>

            <br>

            <div class="row">

                <div class="col-md-12" style="font-size:15px;">

                    <p><b>Button : </b><button id="idOfButton" title="Click me!!" type="button" onclick="this.style.background='green';">Submit</button></p>

                </div>

            </div>

            <br>

            <div class="row">

                <div class="col-md-12" style="font-size:15px;">

                    <p><b>Radio button : </b>

                        <form action="#">

                            <input id="male" type="radio" name="gender" value="male"> Male 

                            <input id="female" type="radio" name="gender" value="female"> Female

                        </form>

                    </p>

                </div>

            </div>

            <br>

            <div class="row">

                <div class="col-md-12" style="font-size:15px;">

                    <p><b>Checkbox :</b>

                        <form action="#">

                            <input type="checkbox" class="Automation" value="Automation"> Automation Testing

                            <input type="checkbox" class="Performance" value="Performance"> Performance Testing

                        </form>

                    </p>

                </div>

            </div>

            <br>

            <div class="row">

                <div class="col-md-12" style="font-size:15px;">

                    <p><b>Drop down :</b>

                        <select id="testingDropdown">

                            <option id="automation" value="Automation">Automation Testing</option>

                            <option id="performance" value="Performance">Performance Testing</option>

                            <option id="manual" value="Manual">Manual Testing</option>

                            <option id="database" value="Database">Database Testing</option>

                        </select>

                    </p>

                </div>

            </div>

            <br>

            <div class="row">

                <div class="col-md-12" style="font-size:15px;">

                    <p><button id="dblClkBtn" ondblclick="alert('hi, Yiibai Testing');">Double-click to generate alert box</button></p>

                </div>

            </div>

            <br>

            <div class="row">

                <div class="col-md-12" style="font-size:15px;">

                    <p><b>Click button to generate Alert box : </b>

                          <button onclick="alert('hi, Yiibai Testing');">Generate Alert Box</button>

                    </p>

                </div>

            </div>

            <br>

            <div class="row">

                <div class="col-md-12" style="font-size:15px;">

                    <p> <b> Click button to generate Confirm box : </b>

                        <button onclick="generateConfirmBox()">Generate Confirm Box</button>

                    </p>

                    <p id="demo"></p>

                </div>

            </div>

            <br>

            <div class="row">

                <div class="col-md-12" style="font-size:15px;">

                    <p>Drag and drop example- drag the below image on the textbox</p>

                    <div id="targetDiv" ondrop="drop(event)" ondragover="allowDrop(event)" style="width:400px;height:150px;padding:10px;border:1px solid #aaaaaa;"></div>

                    <img id="sourceImage" src="https://www.yiibai.com/static/img/logo.png" alt="yiibai" draggable="true" ondragstart="drag(event)" height="120px">

                </div>

            </div>

            <br>

        </div>

        <script>

            function generateConfirmBox()

            {

                var x;

                var r=confirm("Press a button!");

                if (r==true)

                {

                    x="You pressed OK!";

                }

                else

                {

                    x="You pressed Cancel!";

                }

                document.getElementById("demo").innerHTML=x;

            }

            function allowDrop(ev)

            {

                ev.preventDefault();

            }

            function drag(ev)

            {

                ev.dataTransfer.setData("Text",ev.target.id);

            }

            function drop(ev)

            {

                ev.preventDefault();

                var data=ev.dataTransfer.getData("Text");

                ev.target.appendChild(document.getElementById(data));

            }

        </script>

    </body>

</html>

源码

import org.openqa.selenium.JavascriptExecutor;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.testng.annotations.BeforeMethod;

import org.testng.annotations.Test;

public class Test {

    WebDriver driver;

    @BeforeMethod

    public void Test01(){

        System.setProperty("webdriver.chrome.driver","C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe");

        driver = new ChromeDriver();

        driver.get("file:///C:/Users/Administrator/Downloads/%E6%B5%8B%E8%AF%95%E9%A1%B5%E9%9D%A2.html");

        driver.manage().window().maximize();

        try {

            Thread.sleep(3000);

        } catch (InterruptedException e) {

            e.printStackTrace();

        }

    }

    @Test

    public void Test02(){

        //向下滚动网页500像素

        JavascriptExecutor js = (JavascriptExecutor)driver;

        js.executeScript("scrollBy(0, 500)");

    }

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容