WEB automation (JAVA version)-WebDriver related API

WebDriver related API

  • get (String url) to
    access the specified url page
  • getCurrentUrl ()
    gets the url address of the current page
  • getTitle ()
    Get the title of the current page
  • getPageSource ()
    Get current page source code
  • quit ()
    closes the drive object and all related windows
  • close ()
    Close the current window
  • getWindowHandle ()
    returns the current page handle
  • getWindowHandles ()
    returns all handles of the page opened by the drive object, the pages are different, the handles are different
  • manage ()
    This method can get Options-browser menu operation object
    driver.manage (). window ()
  • navigate object
//获取navigate对象
Navigation navigation = driver.navigate();

//访问指定的url地址
navigation.to(url);

//刷新当前页面
navigation.refresh();

//浏览器回退操作
navigation.back();

//浏览器前进操作
navigation.forward();

Code example

The following are code examples of five WebDriver related APIs:

  • get (String url) to
    access the specified url page
  • getCurrentUrl ()
    gets the url address of the current page
  • getTitle ()
    Get the title of the current page
  • getPageSource ()
    Get current page source code
  • quit ()
    closes the drive object and all related windows
package com.test;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class WebDriverOperate {

	private static ChromeDriver chromeDriver;
	
	public static void main(String[] args) throws InterruptedException {
		openChrome();
		//get(String url)
		//访问指定url页面
		//getCurrentUrl()
		//获取当前页面的url地址
		System.out.println("当前的URL为:"+chromeDriver.getCurrentUrl());
		//getTitle()
		//获取当前页面的标题
		System.out.println("当前的标题为:"+chromeDriver.getTitle());
		//getPageSource()
		//获取当前页面源代码
		//System.out.println("当前页面的源代码为:"+chromeDriver.getPageSource());
		//quit()
		//关闭驱动对象以及所有相关的窗口
		chromeDriver.quit();
	}
	public static void openChrome() {
		System.setProperty("webdriver.chrome.driver", "src/test/resources/chromedriver.exe");
		//1.打开Chrome浏览器
		chromeDriver = new ChromeDriver();
		//2.访问百度
		chromeDriver.get("http://www.baidu.com");
	}
}

The console output is as follows:

当前的URL为:https://www.baidu.com/
当前的标题为:百度一下,你就知道

The code example for closing the current window is as follows:

  • close()
package com.test;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class WebDriverOperate {

	private static ChromeDriver chromeDriver;
	
	public static void main(String[] args) throws InterruptedException {
		openChrome();
		
		//close()
		//关闭当前窗口
		chromeDriver.findElement(By.id("kw")).sendKeys("自动化测试");
		chromeDriver.findElement(By.id("su")).click();
		Thread.sleep(1000);
		chromeDriver.findElement(By.xpath("//a[text()='_百度百科']")).click();;
		Thread.sleep(3000);
		chromeDriver.close();		
	}
	public static void openChrome() {
		System.setProperty("webdriver.chrome.driver", "src/test/resources/chromedriver.exe");
		//1.打开Chrome浏览器
		chromeDriver = new ChromeDriver();
		//2.访问百度
		chromeDriver.get("http://www.baidu.com");
	}
}

The following two WebDriver related API code examples:

  • getWindowHandle ()
    returns the current page handle
  • getWindowHandles ()
    returns all handles of the page opened by the drive object, the pages are different, the handles are different
package com.test;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class WebDriverOperate {

	private static ChromeDriver chromeDriver;
	
	public static void main(String[] args) throws InterruptedException {
		openChrome();		
		chromeDriver.findElement(By.id("kw")).sendKeys("自动化测试");
		chromeDriver.findElement(By.id("su")).click();
		System.out.println("新窗口打开前的句柄:"+chromeDriver.getWindowHandle());
		System.out.println("新窗口打开前的所有句柄:"+chromeDriver.getWindowHandles());		
		Thread.sleep(1000);
		chromeDriver.findElement(By.xpath("//a[text()='_百度百科']")).click();;
		Thread.sleep(3000);
		//chromeDriver.close();
		System.out.println("新窗口打开后的句柄:"+chromeDriver.getWindowHandle());
		System.out.println("新窗口打开后的所有句柄:"+chromeDriver.getWindowHandles());
		
	}
	public static void openChrome() {
		System.setProperty("webdriver.chrome.driver", "src/test/resources/chromedriver.exe");
		//1.打开Chrome浏览器
		chromeDriver = new ChromeDriver();
		//2.访问百度
		chromeDriver.get("http://www.baidu.com");
	}
}

The console output is as follows:

新窗口打开前的句柄:CDwindow-F7B1053E51D1A1A91B07F35FC4BB1D39
新窗口打开前的所有句柄:[CDwindow-F7B1053E51D1A1A91B07F35FC4BB1D39]
新窗口打开后的句柄:CDwindow-F7B1053E51D1A1A91B07F35FC4BB1D39
新窗口打开后的所有句柄:[CDwindow-F7B1053E51D1A1A91B07F35FC4BB1D39, CDwindow-179907C8D252AD85E36ABE019954C314]

The following is a code example of this WebDriver related API:

  • manage ()
    This method can get Options-browser menu operation object
    driver.manage (). window ()
package com.test;

import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.WebDriver.Options;
import org.openqa.selenium.chrome.ChromeDriver;

public class WebDriverOperate {

	private static ChromeDriver chromeDriver;
	
	public static void main(String[] args) throws InterruptedException {
		openChrome();	
		Options options = chromeDriver.manage();
		//全屏
		//options.window().fullscreen();
		//最大化窗口
		//options.window().maximize();
		Dimension dimension = options.window().getSize();
		System.out.println("窗口的高度:"+dimension.getHeight());
		System.out.println("窗口的宽度:"+dimension.getWidth());
		System.out.println(options.window().getPosition().getX());
		System.out.println(options.window().getPosition().getY());	
	}
	public static void openChrome() {
		System.setProperty("webdriver.chrome.driver", "src/test/resources/chromedriver.exe");
		//1.打开Chrome浏览器
		chromeDriver = new ChromeDriver();
		//2.访问百度
		chromeDriver.get("http://www.baidu.com");
	}

}

The console output is as follows:

窗口的高度:660
窗口的宽度:1050
10
10

The code example of the navigate object is as follows:

package com.test;

import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.WebDriver.Navigation;
import org.openqa.selenium.WebDriver.Options;
import org.openqa.selenium.chrome.ChromeDriver;

public class WebDriverOperate {

	private static ChromeDriver chromeDriver;
	
	public static void main(String[] args) throws InterruptedException {
		openChrome();
			
		//navigate对象
		Navigation navigation = chromeDriver.navigate();
		Thread.sleep(2000);
		//访问JD
		navigation.to("https://www.jd.com");
		//刷新网页
		Thread.sleep(2000);
		navigation.refresh();
		//回退
		Thread.sleep(2000);
		navigation.back();
		//前进
		Thread.sleep(2000);
		navigation.forward();
	}
	
	public static void openChrome() {
		System.setProperty("webdriver.chrome.driver", "src/test/resources/chromedriver.exe");
		//1.打开Chrome浏览器
		chromeDriver = new ChromeDriver();
		//2.访问百度
		chromeDriver.get("http://www.baidu.com");
	}
}
Published 73 original articles · won praise 2 · Views 3154

Guess you like

Origin blog.csdn.net/anniewhite/article/details/105339485