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