关于Selenium网页模拟访问测试工具的使用

Selenium的官方下载地址:http://www.seleniumhq.org/

这里使用2.45版本,对于Java工程,需要导入的jar包有:

selenium-java-2.45.0-srcs.jar
selenium-java-2.45.0.jar
selenium-server-standalone-2.45.0.jar

除此以外,还需下载浏览器驱动工具,Chrome浏览器驱动工具下载地址:http://chromedriver.storage.googleapis.com/index.html,IE浏览器驱动工具下载地址:http://selenium-release.storage.googleapis.com/index.html

这里要注意几个问题:

1)建议使用Chrome,本测试使用Chrome,IE使用规则请百度

2)Chrome浏览器驱动工具要根据自己Chrome浏览器版本来确定,如果不对,会出现浏览器地址栏只显示“data:,”之类的奇怪错误,我使用的Chrome是55版本,使用的Chrome浏览器驱动工具为2.26

3)IE浏览器驱动工具要根据Selenium版本选择,具体请百度

测试代码如下:

1)直接显示所有网页内容
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class Exec {

	public static void main(String[] args) {
		System.setProperty("webdriver.chrome.driver",
				"C:\\temp\\chromedriver.exe");

		WebDriver driver = new ChromeDriver();
		driver.get("http://www.baidu.com");
		WebElement webElement = driver.findElement(By.xpath("/html"));
		System.out.println(webElement.getAttribute("outerHTML"));
		System.out.println(webElement.getText());

		driver.quit();
	}
}


2)获取网页指定id的内容
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class Exec {

	public static void main(String[] args) {
		System.setProperty("webdriver.chrome.driver",
				"C:\\temp\\chromedriver.exe");

		WebDriver driver = new ChromeDriver();
		driver.get("http://www.baidu.com");
		WebElement webElement = driver.findElement(By.id("su"));
		System.out.println(webElement.getAttribute("value"));

		driver.quit();
	}
}


3)自动百度搜索:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class Exec {

	public static void main(String[] args) {
		System.setProperty("webdriver.chrome.driver",
				"C:\\temp\\chromedriver.exe");

		WebDriver driver = new ChromeDriver();
		driver.get("http://www.baidu.com");
		WebElement webElement = driver.findElement(By.xpath("/html"));
		driver.findElement(By.name("wd")).sendKeys("南京财经大学");
		driver.findElement(By.id("su")).click();		
		try {
			Thread.sleep(10000);			
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println(webElement.getText());
		driver.quit();
	}
}


4)自动邮箱登录
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class Exec {

	public static void main(String[] args) {
		System.setProperty("webdriver.chrome.driver",
				"C:\\temp\\chromedriver.exe");

		WebDriver driver = new ChromeDriver();
		driver.get("http://mail.njue.edu.cn/");
		
		driver.findElement(By.id("uid")).sendKeys("XXXX");
		driver.findElement(By.name("password")).sendKeys("XXXX");
		driver.findElement(By.name("action:login")).click();
		try {
			Thread.sleep(10000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		driver.quit();
	}
}

XXXX要根据实际用户密码来更改

关于Selenium网页模拟访问测试工具的使用》有1个想法

发表评论

邮箱地址不会被公开。 必填项已用*标注