• 首页
  • 狐文
  • 狐图
  • About
狐窝
OvO
  1. 首页
  2. 未分类
  3. 正文

WebDriver中页面滚动(scrolling)

2017年08月10日 70点热度 0人点赞 0条评论

2014 from---http://blog.csdn.net/tanzhangwen/article/details/18733557

这里面主要由链接(http://selenium-tutorial.blogspot.sg/2013/02/scroll-page-webdriver.html)整理过来,主要回答了如何用webdriver来滚动页面,滚上滚下,滚到指定元素以及怎么识别滚动条等。

1.  利用selenium中的focus(locator)函数实现(Link)

view plain copy

  1. @Test
  2. public void testFocus() throws Exception {
  3.     selenium.open("/");
  4.     selenium.windowMaximize();
  5.     selenium.type("q", "selenium wiki");
  6.     selenium.click("btnG");
  7.     //Waiting for an element
  8.     for (int second = 0;; second++) {
  9.         if (second >= 60) fail("timeout");
  10.         try {
  11.             if (selenium.isElementPresent("link=Selenium Overview - Wiki - Liferay.com"))
  12.                 break;
  13.             } catch (Exception e) {}
  14.         Thread.sleep(1000);
  15.         }
  16.     //Set focus on element --then page will scroll down --focus will be on that element
  17.     selenium.focus("link=Selenium Overview - Wiki - Liferay.com");
  18.     //To capture a screenshot
  19.     selenium.captureScreenshot("c:/naga/screenshot.png");
  20.     Thread.sleep(10000);
  21. }

 

2. 利用webdriver执行脚本直接滚动到指定坐标位置(LinkLink2)

view plain copy

  1. public static void test1() throws InterruptedException{
  2.     WebDriver driver = new FirefoxDriver();
  3.     driver.get("http://www.nytimes.com/");
  4.     ((JavascriptExecutor)driver).executeScript("scrollTo(0,3000)");
  5.      Thread.sleep(5000L);
  6. }
view plain copy

  1. ((JavascriptExecutor) driver).executeScript("scroll(0,250);");  //-250
  2. ((JavascriptExecutor) driver).executeScript("scroll(250,0);");
  3. ((JavascriptExecutor) driver).executeScript("window.scrollBy(0,250)", "");
  4. ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView();" ,webElement);

 

3. 判断是否有滚动条可以比较窗口高度和滚动条高度(Link)

view plain copy

  1. Selenium.type(xpath of the text-area element,text value);
  2. String str1=Selenium.getEval("this.browserbot.getCurrentWindow().document.getElementsByClassName('cssclassname')[0].clientHeight");
  3. String str = Selenium.getEval("this.browserbot.getCurrentWindow().document.getElementsByClassName('cssclassname')[0].scrollHeight");
  4. int clientHeight = Integer.parseInt(str1);
  5. int scrollHeight = Integer.parseInt(str);
  6. if(clientHeight < scrollHeight){
  7.     System.out.println("Vertical scrollbar appears");
  8. }else{
  9.     System.out.println("Vertical scrollbar is not appear");
  10. }

 

4. 滚动到目标元素的纵坐标位置(Link)

view plain copy

  1. public void scrollAndClick(By by)
  2. {
  3.    WebElement element = driver.findElement(by);
  4.    int elementPosition = element.getLocation().getY();
  5.    String js = String.format("window.scroll(0, %s)", elementPosition);
  6.    ((JavascriptExecutor)driver).executeScript(js);
  7.    element.click();
  8. }

5. 滚动到指定元素位置

view plain copy

  1. WebElement element = webdriver.findElement(locator);
  2. ((Locatable)element).getLocationOnScreenOnceScrolledIntoView();

 

发现最新的webdriver代码已改,上面的函数已经没有了,经过试验一下代码可以替换:

view plain copy

  1. Coordinates coor = ((Locatable)element).getCoordinates();
  2. coor.inViewPort();

 

标签: 暂无
最后更新:2017年08月10日

OvO

狐狸

点赞
< 上一篇
下一篇 >
最新 热点 随机
最新 热点 随机
钛备份或小米系统备份功能备份出来的APK无法正常还原 BBR安装启用 安装docker mitmproxy 调试方法 Android SDK 版本选择 Perdition Mail代理服务器配置 HAProxy SSL代理 Openssl 中间人SSL代理 Stunnel SSL to SSL代理 NGINX反代IMAP配置 各种设备User Agent Torrent下载工具推荐 用 rar2john+hashcat 破解 RAR 文件密码 2020最好的Linux发行版 关闭新版chrome工具栏上拼图图标 Office 下载、安装、激活,有它就够了! Android 非官方的发行版 openwrt添加autoconf-archive
NGINX反代IMAP配置Stunnel SSL to SSL代理Openssl 中间人SSL代理HAProxy SSL代理Perdition Mail代理服务器配置Android SDK 版本选择mitmproxy 调试方法安装dockerBBR安装启用钛备份或小米系统备份功能备份出来的APK无法正常还原
windows环境下mysql5.6数据库的主从同步备份步骤 Intellij Idea 的maven 项目会在 右键菜单 maven reimport 变成jdk 1.5 知识库 : 使用Tesseract识别弱验证码 vim中delete(backspace)键不能向左删除 Ubuntu编译运行ss-redir打造透明代理Wifi环境 Windows 8.1/Server 2012 R2 也可以 KMS激活了 国内外几款好用的网络质量测试工具推荐 2019年的人们如何生成HTTPS证书 Debian防火墙问题 安裝unstable Release Debian方式 MSYS2使用教程 Linux服务器上监控网络带宽的18个常用命令 Windows去重服务如何工作 动态网页数据抓取踩坑分享 租房注意事项 GitHub 上有哪些优秀的 Java 爬虫项目? 【PS色彩】最舒服的色彩搭配 Oracle中查询表的大小、表的占用情况和表空间的大小
标签聚合
yum 网卡 e 密码 git 下载 路由 镜像 安装 blog chrome https ssl chromium linux docker

COPYRIGHT © 2020 狐窝. ALL RIGHTS RESERVED.

THEME KRATOS MADE BY VTROIS