JS控制滚动条的位置:

window.scrollTo(x,y);

竖向滚动条置顶 window.scrollTo(0,0);

竖向滚动条置底 window.scrollTo(0,document.body.scrollHeight)

JS控制TextArea滚动条自动滚动到最下部

document.getElementById('textarea').scrollTop = document.getElementById('textarea').scrollHeight

例子:

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.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert; /**
* @author Hjianhui
* JavaScript.java 2016-08-01
*
*/
public class testScroll{ public static void main(String[] args) {
// TODO Auto-generated method stub
//利用webdriver键入搜索关键字
WebDriver driver = new FirefoxDriver();
try{
driver.get("http://www.baidu.com"); JavascriptExecutor driver_js= (JavascriptExecutor) driver;
//利用js代码键入搜索关键字
driver_js.executeScript("document.getElementById(\"kw\").value=\"yeetrack\""); driver.findElement(By.id("su")).click(); //等待元素页面加载
waitForElementToLoad(driver, 10, By.xpath(".//*[@id='1']/h3/a[1]")); //将页面滚动条拖到底部
driver_js.executeScript("window.scrollTo(0,document.body.scrollHeight)"); //#将页面滚动条拖到顶部
driver_js.executeScript("window.scrollTo(0,0)"); }catch (Exception e){
e.printStackTrace();
}
driver.quit();
} /**
* 在给定的时间内去查找元素,如果没找到则超时,抛出异常
* */
public static void waitForElementToLoad(WebDriver driver, int timeOut, final By By) {
try {
(new WebDriverWait(driver, timeOut)).until(new ExpectedCondition<Boolean>() { public Boolean apply(WebDriver driver) {
WebElement element = driver.findElement(By);
return element.isDisplayed();
}
});
} catch (TimeoutException e) {
Assert.fail("超时!! " + timeOut + " 秒之后还没找到元素 [" + By + "]"); }
}
}

waitForElementToLoad()函数是为了保证页面元素已加载完成,以便执行JS代码

Python代码

from selenium import  webdriver
from selenium.webdriver.support.wait import WebDriverWait
driver = webdriver.Firefox()
driver.get("http://www.baidu.com")
driver.execute_script("document.getElementById(\"kw\").value=\"selenium\"")
WebDriverWait(driver, 10).until(lambda driver : driver.find_element_by_xpath(".//*[@id='container']/div[2]/div/div[2]"))
driver.execute_script("window.scrollTo(0,document.body.scrollHeight)")
driver.quit()
 

selenium webdriver——JS滚动到最底部的更多相关文章

  1. selenium webdriver——JS滚动到指定位置

    1.DOM滚动方法 1.scrollIntoView(alignWithTop)  滚动浏览器窗口或容器元素,以便在当前视窗的可见范围看见当前元素.如果alignWithTop为true,或者省略它, ...

  2. Selenium Webdriver——JS处理rich text(富文本框)

    126邮件正文邮件的rich text 先让selenium切换到iframe中 driver.switchTo().frame(driver.findElement(By.className(&qu ...

  3. selenium webdriver——JS对Input执行输入

    直接设置value属性, 此方法主要应对输入框自动补全以及readonly属性的element,sendkeys不稳定 比如: Demo:用JS输入 import org.openqa.seleniu ...

  4. selenium webdriver——JS操作日历控件

    一般的日期控件都是input标签下弹出来的,如果使用webdriver 去设置日期, 1. 定位到该input 2. 使用sendKeys 方法 比如 但是,有的日期控件是readonly的 比如12 ...

  5. js滚动到顶部底部代码

    <!DOCTYPE HTML> <html> <head> <meta charset=UTF-8> <title>SCROLL</t ...

  6. selenium webdriver 屏幕滚动

    //山寨法 String setscroll = "document.documentElement.scrollTop=100"; JavascriptExecutor jse= ...

  7. Python selenium webdriver设置js操作页面滚动条

    js2 = "window.scrollTo(0,0);" #括号中为坐标 当不知道需要的滚动的坐标大小时: weizhi2 = driver.find_element_by_id ...

  8. selenium,webdriver 执行js语句 对象是百度

    代码要多敲 注释要清晰 最后的两种方法,没有实现我想要的结果 有知道的朋友,给我留言吧 #执行js语句 from selenium import webdriver import time #生成浏览 ...

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

    未经作者允许,禁止转载!!! java selenium webdriver处理JS操作窗口滚动条 java selenium webdriver处理JS操作窗口滚动条 import org.open ...

随机推荐

  1. 雷林鹏分享:Ruby Web Services 应用 - SOAP4R

    Ruby Web Services 应用 - SOAP4R 什么是 SOAP? 简单对象访问协议(SOAP,全写为Simple Object Access Protocol)是交换数据的一种协议规范. ...

  2. poj1087最大流拆点

    刚开始看这题太长了就放着,后来做了之后才发现并不难,就是构造图有点麻烦 一开始写了180行@.@结果tle了,后来想到用map直接访问的话可能会快点,就不用每次循环了 #include<map& ...

  3. h5桌面通知Notification

    H5中的桌面通知Notification 前言: 对于一个前端开发者,逛网页总会留意一些新奇的功能,对于上班总会用到Teambition的我,总是能收到Notification...所以今天就来研究下 ...

  4. JavaScript学习总结(二十二)——JavaScript屏蔽Backspace键

    今天在IE浏览器下发现,当把使用readonly="readonly"属性将文本框设置成只读<input type="text" readonly=&qu ...

  5. 【python】BytesIO与串化

    一共有以下几个概念 1.类文件: File(path), open(path), BytesIO(), ... 文件读之前要seek(0) 2.字符串: file.read() 3.对象: dict, ...

  6. HDU 3986

    http://acm.hdu.edu.cn/showproblem.php?pid=3986 从开始的最短路里依次删一条边,求新的最短路,求最长的最短路 删边操作要标记节点以及节点对应的边 #incl ...

  7. BZOJ4917: [Lydsy1706月赛]Hash Killer IV(模拟)

    4917: [Lydsy1706月赛]Hash Killer IV Time Limit: 1 Sec  Memory Limit: 256 MBSubmit: 327  Solved: 140[Su ...

  8. windows常用DLL及作用

    Kernel.dll:内存,硬盘灯硬件管理的相关函数. gdi32.dll:图形显示相关函数(LoadImage,GetPixel,StretchBlt,Ellipse). user32.dll:wi ...

  9. 【转】Ubuntu12.04安装YouCompleteMe插件

    原文网址:http://m.blog.csdn.net/blog/unhappypeople/19160243 以前用的都是ctags+omnicomplete+acp的方式,这次换成clang自解析 ...

  10. ReportViewer 2010 打印预览,用鼠标快速切换显示比例时报错:存储空间不足,不能处理此命令

    CreateCompatibleDIB 存储空间不足 无法处理此命令 安装 ReportViewer 2010 sp1 即可.