0

这是我的代码,它打开网页并将输入文本输入文本字段并尝试自动进一步浏览。第一个代码:element.sendKeys(Keys.ENTER); 正在工作,但紧随其后的下两行对我的程序没有影响。我被困在这里。请帮助我

public class GoogleSuggest {
    public static void main(String[] args) {

        WebDriver driver = new FirefoxDriver();

        // And now use this to visit Google
        driver.get("http://www.google.com");

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

        // Enter something to search for
        element.sendKeys("vtu results");

        element.sendKeys(Keys.ENTER);

        element.sendKeys(Keys.TAB);
        element.sendKeys(Keys.Enter);


        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

        (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
            public Boolean apply(WebDriver d) {
                return d.getTitle().toLowerCase().startsWith("v");
            }
        });

        System.out.println("Page title is: " + driver.getTitle());
    }
}
4

1 回答 1

0

不清楚您想要执行的所有操作,但为我工作。试试这个:

    driver.get("http://www.google.com");
    WebElement element = driver.findElement(By.name("q"));
    Actions action = new Actions(driver);
    element.sendKeys("result");
    action.sendKeys(element,Keys.ENTER).perform();
    action.sendKeys(Keys.TAB);
    Thread.sleep(2000);
    action.sendKeys(Keys.ENTER).perform();
    Thread.sleep(2000);
    System.out.println("Page title is: " + driver.getTitle());
    (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
        public Boolean apply(WebDriver d) {
            return d.getTitle().toLowerCase().startsWith("R");
        }
    });

    System.out.println("Page title is: " + driver.getTitle());
}
于 2013-07-06T09:13:42.220 回答