zoukankan      html  css  js  c++  java
  • 使用selenium控制滚动条(非整屏body)

    方法原理:

        (1)使用jQuery CSS 操作 - scrollTop() 方法,设置 <div> 元素中滚动条的垂直偏移,语法:$(selector).scrollTop(offset);
      (2)若要控制滚动条水平偏移,请使用方法scrollLeft(),语法:$(selector).scrollLeft(offset);
    其中selector表示选择器,offset表示偏移量。
      
    样例页面MyJsp.jsp:
    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>My JSP 'MyJsp.jsp' starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
        <script type="text/javascript" src="js/jquery.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function(){
      $(".btn1").click(function(){
        $("#test div").scrollTop(100);
      });
      $(".btn2").click(function(){
        alert($("div").scrollTop()+" px");
      });
    });
    </script>
    </head>
    
    <body>
        <div id="test">
        <div 
            style="border:1px solid black;200px;height:200px;overflow:auto">
            This is some text. This is some text. This is some text. This is some
            text. This is some text. This is some text. This is some text. This is
            some text. This is some text. This is some text. This is some text.
            This is some text. This is some text. This is some text. This is some
            text. This is some text. This is some text. This is some text. This is
            some text. This is some text. This is some text. This is some text.
            This is some text. This is some text. This is some text. This is some
            text. This is some text. This is some text. This is some text. This is
            some text. This is some text. This is some text. This is some text.
            This is some text. This is some text. This is some text.</div>
            </div>
        <button class="btn1">把 scroll top offset 设置为 100px</button>
        <br />
        <button class="btn2">获得 scroll top offset</button>
    
    </body>
    </html>
    View Code

    selenium代码:

    本例中仅控制滚动条垂直移动

    import org.junit.After;
    import org.junit.Before;
    import org.junit.Test;
    import org.openqa.selenium.By;
    import org.openqa.selenium.JavascriptExecutor;
    import org.openqa.selenium.TimeoutException;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.ie.InternetExplorerDriver;
    import org.openqa.selenium.support.ui.ExpectedCondition;
    import org.openqa.selenium.support.ui.WebDriverWait;
    
    public class TestScroll {
        private WebDriver driver;
        @Before
        public void setUp(){
            //login
            System.setProperty("webdriver.ie.driver", "C:\Program Files (x86)\Internet Explorer\IEDriverServer.exe");
            driver = new InternetExplorerDriver();
            String url = "http://localhost:8080/test/MyJsp.jsp";
         //l打开测试页面 driver.get(url); WebDriverWait wait
    = new WebDriverWait(driver, 5); try { WebElement webElement = wait.until(new ExpectedCondition<WebElement>() { @Override public WebElement apply(WebDriver webDriver) { return webDriver.findElement(By.id("test")); } }); if(webElement==null){ System.out.println("页面还未加。。。。"); }else{ System.out.println("页面加载完成~~~~~~~~~~~"); JavascriptExecutor js = (JavascriptExecutor)driver;
              //执行js脚本,控制滚动条滚动 js.executeScript(
    "$('#test div').scrollTop(100);"); Thread.sleep(10000); } } catch (TimeoutException e) { System.out.println("打不开链接。。。。。"); e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } @Test public void testLogic(){ } @After public void tearDown(){ if(driver!=null){ driver.quit(); } } }
  • 相关阅读:
    Windows 8(64位)如何搭建 Android 开发环境与真机测试(转)
    C# winform写入和读取TXT文件
    winform利用ImageList控件和ListView控件组合制作图片文件浏览器
    Linux 桌面玩家指南:01. 玩转 Linux 系统的方法论
    这些年一直记不住的 Java I/O
    深入理解 JavaScript,以及 Linux 下的开发调试工具
    在 Ubuntu 中使用 Visual Studio Code
    像黑客一样使用 Linux 命令行
    JAAS 是个什么梗
    Java 开发主流 IDE 环境体验
  • 原文地址:https://www.cnblogs.com/splvxh/p/4234043.html
Copyright © 2011-2022 走看看