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(); } } }
  • 相关阅读:
    将自己数据转化为cifar10支持的lmdb
    python实现cifar10数据集的可视化
    Python OS 文件/目录方法
    象棋AI算法(二)
    象棋AI算法(一)
    围棋人机大战中阿尔法狗原理解析,左右互搏,青出于蓝而胜于蓝?
    电脑开机停留在主板设置界面,进不了系统
    哪一种编程语言适合人工智能?
    BIOS设置图解教程-看完就没有不明白的了
    关于AndroidStudio的打包数字签名以及多渠道发布
  • 原文地址:https://www.cnblogs.com/splvxh/p/4234043.html
Copyright © 2011-2022 走看看