java selenium webdriver处理JS操作窗口滚动条

未经作者允许,禁止转载!!!

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class JS20161212 {

    public static void main(String[] args) throws InterruptedException {
        // TODO Auto-generated method stub
        System.setProperty("webdriver.chrome.driver","C:\\Training\\Automation\\Software\\drivers\\chromedriver_win32\\chromedriver.exe");
        WebDriver driver=new ChromeDriver();
        driver.get("https://www.hao123.com/");
        driver.manage().window().maximize();
        Thread.sleep(2000);

        JavascriptExecutor js = (JavascriptExecutor)driver;
        js.executeScript("scrollTo(0,10000)");----------------------------------------------向下拉到底
        Thread.sleep(2000);
        js.executeScript("scrollBy(0, 0-document.body.scrollHeight *99/100)");--------------向上拉到顶
        Thread.sleep(2000);
        js.executeScript("scrollTo(0,100000)");---------------------------------------------向下拉到底
        Thread.sleep(2000);
        js.executeScript("scrollTo(0,1)");--------------------------------------------------向上拉到顶
        Thread.sleep(2000);
        js.executeScript("scrollTo(0,1000)");
        Thread.sleep(2000);
        js.executeScript("scrollBy(0, 0-document.body.scrollHeight *1/2)");-----------------拉到中间
        Thread.sleep(2000);
        js.executeScript("scrollBy(0, 0-document.body.scrollWidht *1/2)");------------------左右拉到中间

//      ((JavascriptExecutor)driver).executeScript("scrollTo(0,10000)");----------------精简后的代码
//      Thread.sleep(2000);
//      ((JavascriptExecutor)driver).executeScript("scrollTo(0,1)");

    }

}
时间: 2024-07-30 10:19:24

java selenium webdriver处理JS操作窗口滚动条的相关文章

selenium webdriver 截屏操作

有时候我们需要进行截屏操作,特别是遇到一些比较重要的页面信息(出现错误)或者出现不同需要进行对比时, 我们就需要对正在处理的页面进行截屏! 未经作者允许,禁止转载! package test_wait20161205; import java.io.File; import java.io.IOException; import org.apache.commons.io.FileUtils; import org.openqa.selenium.OutputType; import org.o

如何用selenium webdriver 捕获js error

### 问题 捕捉页面上js error ### 解决办法 从Selenium webdriver log 中解析 # -*- coding:utf8 -*- import unittest from selenium import webdriver from selenium.webdriver.common.desired_capabilities import DesiredCapabilities class CaptureJSError(unittest.TestCase): @cl

selenium - webdriver keys 键盘操作

webdriver的Keys()类,提供了几乎所有按键的方法,常用的如下: Keys.BACK_SPACE     删除键 Keys.SPACE               空格键 Keys.TAB                   制表键(Tab) Keys.ESCAPE             回退键(Esc) Keys.ENTER               回车键(Enter) Keys.CONTROL          Ctrl键 ps:元素的send_keys()方法,可以模拟键盘

js获取窗口滚动条高度、窗口可视范围高度、文档实际内容高度、滚动条离浏览器底部的高度

1.获取窗口可视范围的高度 1 //获取窗口可视范围的高度 2 function getClientHeight(){ 3 var clientHeight=0; 4 if(document.body.clientHeight&&document.documentElement.clientHeight){ 5 var clientHeight=(document.body.clientHeight<document.documentElement.clientHeight)?doc

[转]java selenium webdriver实战 应用小结

原文链接:http://www.cnblogs.com/itliucheng/p/5578788.html 部分api 1.访问网站 driver.get("http://www.baidu.com"); 或者 driver.navigate().to("http://www.baidu.com"); 2.操作浏览器窗口 //声明一个point对象,两个150表示浏览器的位置相对于屏幕的左上角(0,0)的横坐标距离和纵坐标距离 Point point = new P

selenium.webdriver 高亮显示当前操作的元素

高亮显示当前webdriver正在操作的元素,可以使用js代码来完成,两种写法: driver.execute_script('arguments[0].style.border="2px solid red";', ele) driver.execute_script('arguments[0].setAttribute("style", "border: 2px solid red");', ele) ele是通过driver定位到的元素,传

java selenium webdriver实战 页面元素定位

自动化测试实施过程中,测试程序中常用的页面操作有三个步骤 1.定位网页上的页面元素,并存储到一个变量中 2.对变量中存储的页面元素进行操作,单击,下拉或者输入文字等 3.设定页面元素的操作值,比如,选择下拉列表中的那个下拉列表或者输入框中输入什么值 其中定位页面元素是三步骤的第一步,本篇介绍常用的定位方法 webDriver对象的findElement函数用于定位一个页面元素,findElements函数用户定位多个页面元素,定位的页面元素使用webElement对象进行存储 常用的方法有: 1

java+selenium+new——模拟键盘操作——复制、粘贴——action类

package rjcs; import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.interactions.Actions; public class a { public static voi

[selenium webdriver Java]处理弹出窗口

Selenium WebDriver测试弹出窗口,包括识别弹出窗口,将driver转到新的窗口,在新的串钩中执行而是步骤,然后再转换到最初的窗口. 通过名称(name)识别和处理: Selenium WebDriver允许我们通过name属性或窗口的句柄来识别窗口,然后通过WebDriver.switchTo().window()方法在不同的窗口之间进行切换. window name属性的定义 name 属性可设置或得到窗口的名称,其值为字符串.语法如下: 1 window.name = [na