用JAVA写的一个自动化简单示例!

public class lianxi {

public static void main(String[] args) throws InterruptedException {
	//System.getProperty("webDriver.firefox.bin","C:\\Program Files\\Mozilla Firefox\\firefox.exe");
	System.setProperty("webdriver.gecko.driver", "D:\\jmeter\\geckodriver.exe");
	WebDriver driver=new FirefoxDriver();
	driver.get("https://vip.iqiyi.com/");
	driver.findElement(By.id("J-header-search-input")).clear();
	driver.findElement(By.id("J-header-search-input")).sendKeys("电影");
	driver.findElement(By.id("J-search-btn")).click();
	Thread.sleep(3000);
	String handle = driver.getWindowHandle(); //获得当前窗口
	for (String handles : driver.getWindowHandles()) {  
        if (handles.equals(handle))  
            continue;  
        driver.switchTo().window(handles);//driver赋予当前窗口  
    } 	
	driver.findElement(By.xpath("//a[contains(@href, '19rr8f187k')]")).click();;
}

}

找网页驱动,下载ELE版本花了不少时间,还是对xpath语法不怎么熟悉。这是在爱奇艺上播放二代妖精的代码。这里需要注意的是当页面打开新窗口,需要给页面一点缓冲的时间,这里调用了线程。


版权声明:本文为qq_35108756原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。