简单实现优酷搜索,并打印出搜索结果

2012-05-31  付民 

[如需转载,请在转载时注明出处,并保证本文的完整性]

package com.test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

public class TestYouKu{
    public static void main(String[] args) {
        
        WebDriver driver= new FirefoxDriver();   //创建一个WebDriver实例,启动firefox浏览器
        
         driver.get("http://www.youku.com");     // 访问优酷
       
        WebElement element= driver.findElement(By.name("q"));    // 找到文本框

         element.sendKeys("泡芙小姐");     // 输入搜索关键字
        
         element.submit();   //提交表单 WebDriver会自动从表单中查找提交按钮并提交
        
        System.out.println("Page title is: " + driver.getTitle());   // 检查页面title
        
        (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
             public Boolean apply(WebDriver d) {
                 return d.getTitle().toLowerCase().startsWith("泡芙小姐");
             }
        });       // 优酷查询结果是通过javascript动态呈现的.
                  // 设置页面等待10秒超时
        
        System.out.println("Page title is: " + driver.getTitle());   // 显示查询结果title
       
         driver.quit();   //关闭浏览器
    }
}

440°/4325 人阅读/8 条评论 发表评论

李艳艳  2012-05-31


赵国钧  2012-05-31

求教育....


么明明  2012-05-31


小窝  2012-05-31

实用贴


付民  2012-05-31

小窝: 实用贴


付民  2012-05-31

李艳艳: 顶


付民  2012-05-31

赵国钧: 求教育....


付民  2012-05-31

么明明:


登录 后发表评论