WebDriver高级应用实例(4)

  4.1操作web页面的滚动条

  被测网页的网址:

  http://v.sogou.com

  Java语言版本的API实例代码 

  import org.testng.annotations.Test;
  import org.testng.annotations.BeforeMethod;

  import javax.swing.event.TreeWillExpandListener;

  import org.openqa.selenium.By;
  import org.openqa.selenium.JavascriptExecutor;
  import org.openqa.selenium.WebDriver;
  import org.openqa.selenium.WebElement;
  import org.openqa.selenium.chrome.ChromeDriver;
  import org.testng.annotations.AfterMethod;

  public class scrolling {
    WebDriver driver;
    String url ="http://v.sogou.com";
    //priority = 1 表示测试用例的第一优先级
    @Test(priority = 1)
  public void scrollingToBottomofAPage() {
    //将页面滚动至页面最下方
    ((JavascriptExecutor)driver).executeScript("window.scrollTo(0,document.body.scrollHeight)");
    //设置3秒停顿验证滚动条是否移动至指定位置
    try {
      Thread.sleep(3000);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }
  @Test(priority = 2)
  public void scrollingToElementofAPage(){
    //找到标签文字为电视剧的标签
    WebElement element = driver.findElement(By.xpath("//*[@id=‘container‘]//a[text()=‘电视剧‘]"));
    //使用scrollIntView()函数。将滚动条移至元素所在的位置
    ((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView();",element);
    try {
      Thread.sleep(3000);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }
  @Test(priority = 3)
  public void scrollingByCoordinatesofAPage(){
    //将页面滚动条向下移动800个像素
    ((JavascriptExecutor)driver).executeScript("window.scrollBy(0,800)");
    try {
      Thread.sleep(3000);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }
  @BeforeMethod
  public void beforeMethod() {
    System.setProperty("webdriver.chrome.driver", "D:\\WebDriver\\chromedriver_win32\\chromedriver.exe");
    driver = new ChromeDriver();
    //最大化浏览器
    driver.manage().window().maximize();
    driver.get(url);
  }

  @AfterMethod
  public void afterMethod() {
    driver.quit();
  }

 }

原文地址:https://www.cnblogs.com/z-zzz/p/10515576.html

时间: 2024-08-07 21:48:05

WebDriver高级应用实例(4)的相关文章

WebDriver高级应用实例(10)

10.1控制HTML5语言实现的视频播放器 目的:能够获取html5语言实现的视频播放器视频文件的地址.时长.控制进行播放暂停 被测网页的网址: http://www.w3school.com.cn/tiy/t.asp?f=html5_video_all Java语言版本的API实例代码 package cn.html5; import org.testng.annotations.Test; import org.testng.annotations.BeforeMethod; import

Selenium WebDriver高级用法

Selenium GitHub地址 选择合适的WebDrvier WebDriver是一个接口,它有几种实现,分别是HtmlUnitDrvier.FirefoxDriver.InternetExplorerDriver.ChromeDriver.OperaDriver,除了 InternetExplorerDriver只能在Windows平台运行,其他WebDriver均能跨平台. 如果追求运行速度,HtmlUnitDriver是首选,但是它没有运行界面,不能实时看到运行效果.如果想看到运行效果

Oracle 10g高级复制实例(多主体复制)(转载)

Oracle 10g高级复制实例(多主体复制) 前言: 配置了三次,两次都是单向.最后一次终于是双向的了.也算一点有点成功的喜悦.喜悦?喜悦确实谈不上.这么简单的东西弄了这么久觉得惭愧.网络上的资料一大堆.不过他们写的好象都有些随意.有的时候一点点错误可以使你只有重来.我现在重新写一次.只提供给初学者.配置的高手的就不要开腔了.因为实在不好意思拿出来见人. 信息: 主体定义数据库:testdb.geong.com(弄清楚别弄错了.) 主体数据库:backdb.geong.com 操作系统:  x

webdriver高级应用- 浏览器中新开标签页(Tab)

#encoding=utf-8 import unittest from selenium import webdriver import time import win32api, win32con VK_CODE ={'ctrl':0x11, 't':0x54, 'tab':0x09} # 键盘键按下 def keyDown(keyName): win32api.keybd_event(VK_CODE[keyName], 0, 0, 0) # 键盘键抬起 def keyUp(keyName)

webdriver高级应用- 启动带有用户配置信息的firefox浏览器窗口

由于WebDriver启动FireFox浏览器时会启用全新的FireFox浏览器窗口,导致当前机器的FireFox浏览器已经配置的信息在测试中均无法生效,例如已经安装的浏览器插件.个人收藏夹等.为了解决此问题,自动化测试脚本中需要使用指定的配置信息来启动FireFox浏览器窗口. 1.生成用户自定义的FireFox浏览器配置文件: 在CMD中使用cd命令进入firefox.exe文件所在目录(比如:C:\Program Files\Mozilla Firefox),并输入firefox.exe

webdriver高级应用- 右键另存为下载文件

1.要使用右键另存,需要先按照第三方工具AutoIt: 链接: https://pan.baidu.com/s/12aBBhOOTmyQpH9hukt0XGA 密码: fcdk 2.创建一个名为loadFile.au3的AutoItScript编辑器,内容如下: 新建一个名为loadFile.au3的AutoItScript编辑器,文件具体内容如下: ;ControlFocus("title","text",controlID) ;表示将焦点切换到标题为title窗

Python3 Selenium自动化web测试 ==> 第八节 WebDriver高级应用 -- 结束Windows中浏览器的进程

学习目的: 掌握WebDriver的高级应用 正式步骤: # -*- coding:utf-8 -*- from selenium import webdriver from selenium.webdriver import ActionChains from selenium.webdriver.support.ui import Select from selenium.webdriver.common.keys import Keys from selenium.common.excep

2015/12/13 --高级javascript实例和部分javascript对象实例

<html> <head> <script type = "text/javascript"> //替换字符串中的字符--replace()方法 var str = "Do you love me?"; document.write(str.replace (/me/who)); //查找字符串中指定的字符,若找到,返回该字符.---match()方法 var str = "hello kitty"; docu

Selenium WebDriver java 简单实例

开发环境 JDK 下载地址: http://www.oracle.com/technetwork/java/javase/downloads/index.html Eclipse: 下载地址:http://www.eclipse.org/downloads/ Selenium jar包 (这里用的是:selenium-Java-2.45.0.zip ,selenium-server-standalone-2.45.0.jar) 下载地址:http://code.google.com/p/sele