【转载保存】Java+Selenium使用

环境搭建:https://blog.csdn.net/u011541946/article/details/72898514
环境搭建问题解决:https://blog.csdn.net/u010366748/article/details/72872190

package server;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

public class Server {
	public static void webDriver(String url) {
		// 初始化一个chrome浏览器实例,实例名称叫driver
		System.setProperty("webdriver.chrome.driver",
				"F:\\chromedriver.exe");
		ChromeOptions options = new ChromeOptions();
		// 设置chrome浏览器的参数,使其不弹框提示(chrome正在受自动测试软件的控制)
		options.addArguments("disable-infobars");
		WebDriver driver = new ChromeDriver(options);
		// 设置超时
		driver.manage().timeouts().implicitlyWait(8, TimeUnit.SECONDS);
		// 最大化窗口
//				driver.manage().window().maximize();
		// 设置隐性等待时间
		driver.manage().timeouts().implicitlyWait(8, TimeUnit.SECONDS);
		// get()打开一个站点
		driver.get(url);
		// 如果动态网页可以等待js加载完
//		        try {
//					Thread.sleep(1000l);
//				} catch (InterruptedException e) {
//					e.printStackTrace();
//				}
		// 得到网页源代码
		String html = driver.getPageSource();
		System.out.println(html);
		// 获取页面的.gn_search_v2 元素.不存在,等待出现
		driver.findElement(By.id("kw")).sendKeys("你好");
		// 如果想看浏览器中的效果就不需要退出
//				driver.quit();
	}

	public static void main(String[] args) {
		webDriver("https://www.baidu.com");
	}
}

猜你喜欢

转载自blog.csdn.net/dreamzuora/article/details/83821836