selenium2经典例子-如何设置等待时间

上一篇 / 下一篇  2012-04-29 22:23:50

packageorg.openqa.selenium.example;

importorg.openqa.selenium.By;
importorg.openqa.selenium.WebDriver;
importorg.openqa.selenium.WebElement;
importorg.openqa.selenium.firefox.FirefoxDriver;
importorg.openqa.selenium.support.ui.ExpectedCondition;
importorg.openqa.selenium.support.ui.WebDriverWait;

publicclassSelenium2Example {
   publicstaticvoidmain(String[]args){
       // Create a new instance of the Firefox driver
       // Notice that the remainder of the code relies on the interface,
       // not the implementation.
       WebDriverdriver=newFirefoxDriver();

       // And now use this to visit Google
       driver.get("http://www.google.com");
       // Alternatively the same thing can be done like this
       // driver.navigate().to("http://www.google.com");

       // Find the text input element by its name
       WebElementelement=driver.findElement(By.name("q"));

       // Enter something to search for
       element.sendKeys("Cheese!");

       // Now submit the form. WebDriver will find the form. for us from the element
       element.submit();

       // Check the title of the page
       System.out.println("Page title is: "+driver.getTitle());
       
       // Google's search is rendered dynamically with JavaScript.
       // Wait for the page to load, timeout after 10 seconds
       (newWebDriverWait(driver,10)).until(newExpectedCondition<Boolean>(){
           publicBooleanapply(WebDriverd){
               returnd.getTitle().toLowerCase().startsWith("cheese!");
           }
       });

       // Should see: "cheese! - Google Search"
       System.out.println("Page title is: "+driver.getTitle());
       
       //Close the browser
       driver.quit();
   }
}

TAG:

MillionRoses 引用 删除 dian106106   /   2012-05-25 15:13:15
for(int second=0;;second++){
                        if(second>=60) break;
                        if(driver.findElement(By.linkText("Hi, Xiaoying Wu")).isDisplayed()) {
                                System.out.println(second);
                                break;
                        }
                        try {
                                Thread.sleep(1000);
                        } catch (InterruptedException e) {
                                e.printStackTrace();
                        }
                }
MillionRoses 引用 删除 dian106106   /   2012-05-09 10:08:21
WebDriverWait wait = new WebDriverWait(driver,10);                                               
             wait.until(new ExpectedCondition <WebElement>(){
                        @Override
                        public WebElement apply(WebDriver dr) {
                                return dr.findElement(By.linkText("Make xinmsn my homepage"));
                        }}).click();
MillionRoses 引用 删除 dian106106   /   2012-05-05 11:16:39
try {
                        Thread.sleep(3000);
                } catch (InterruptedException e) {
                        e.printStackTrace();
                }
MillionRoses 引用 删除 dian106106   /   2012-04-29 22:42:32
以下是Explicit Waits
其实用Implicit Waits 更简洁些:
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
MillionRoses 引用 删除 dian106106   /   2012-04-29 22:27:03
(newWebDriverWait(driver,10)).until(newExpectedCondition<Boolean>(){
           publicBooleanapply(WebDriverd){
               returnd.getTitle().toLowerCase().startsWith("cheese!");
           }
       });

你可以用getTitle  getURL startsWith  endsWith 其实大小写用的比较少
 

评分:0

我来说两句

日历

« 2024-05-10  
   1234
567891011
12131415161718
19202122232425
262728293031 

数据统计

  • 访问量: 74579
  • 日志数: 80
  • 建立时间: 2012-04-12
  • 更新时间: 2013-05-21

RSS订阅

Open Toolbar