急急急,java模拟用户行为,字文本输入内容后,点击提交,获取最终显示的页面,webdriver,

想做一个自动化测试的程序,在下图输入google的网址

点击按钮

最终跳转到下面这个网页


想要的结果就是用java程序实现,给你一个和上面类似的网址(这个网址点击按钮后会先跳转到一个正在载入的网页,过几秒才跳转到最终想要的网页),程序自动在文本框输入要访问的网址,点击提交后,抓取到最终跳转到的页面,如,上图中的google

package main;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Test {

public static void main(String[] args){
//创建一个WebDriver实例
System.setProperty("webdriver.ie.driver", "Driver\\IEDriverServer.exe");
WebDriver driver= new InternetExplorerDriver();
//访问百度
driver.get("www.baidu.com");
//另一种方式
driver.navigate().to("www.google.com");
//找到文本框
WebElement element = driver.findElement(By.name("q"));
//搜索关键字
element.sendKeys("selenium");
//提交表单 webDriver会自动从表单中查找提交按钮并提交
element.submit();

//检查页面title
System.out.println("页面Title:"+driver.getTitle());
//设置等待时间为10秒
(new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {

@Override
public Boolean apply(WebDriver d) {
// TODO Auto-generated method stub
return d.getTitle().toLowerCase().startsWith("selenium");
}

});

// 显示查询结果title
System.out.println("Page title is: " + driver.getTitle());
//关闭浏览器
driver.quit();

}
}
温馨提示:答案为网友推荐,仅供参考
相似回答