webdriver-超时与等待

来源:互联网 发布:java中权限控制框架 编辑:程序博客网 时间:2024/06/05 09:30
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;


public class selenium_timeout 
{
//超时与等待,implicitlyWait,对象识别超时时间,pageLoadTimeout.页面加载超时时间

static WebDriver driver=new FirefoxDriver();
public static void main(String[] args) throws Exception 
{

//全局设置,为代码中每个对象的识别设置超时时间
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
//全局设置,为代码中每页面的加载设置超时时间
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);

driver.get("http://www.baidu.com/");//打开百度首页的超时时间为30秒
driver.findElement(By.id("kw")).sendKeys("selenium");//识别首页百度搜索框超时时间为30秒
driver.findElement(By.id("su")).click();//识别百度一下按钮的超时时间为30秒

selenium_timeout.openSetLink();
}

public static void openSetLink() throws Exception
{
//如果只为某单个对象的识别超时时间,则调用waitForTime方法
selenium_timeout.waitForTime(By.linkText("设置"));
driver.findElement(By.linkText("设置")).click();
}

//页面对象识别的超时时间方法
public static void waitForTime(By by) throws Exception{
for(int second=1;second<=30;second++)
{
try
{
driver.findElement(by);
break;
}
catch(Exception e)
{
e.printStackTrace();
}
}
Thread.sleep(1000);
}
}
0 0
原创粉丝点击