简体   繁体   English

如何在Java中使用WebDriver执行多个关键动作

[英]How to execute multiple key actions using webdriver with java

This is my code that opens a webpage and feeds input text into textfield and tries to browse further automatically. 这是我的代码,它打开一个网页并将输入的文本输入到textfield中,并尝试自动进行进一步的浏览。 The first code: element.sendKeys(Keys.ENTER); 第一个代码:element.sendKeys(Keys.ENTER); is working but the next two lines immediately after this have no effect in my program. 正在工作,但紧随其后的下两行在我的程序中无效。 I am stuck here. 我被困在这里。 Pls help me 请帮助我

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());
    }
}

Not clear what all actions you want perform but following working for me. 不清楚您要执行的所有操作,但请为我工作。 Try this out: 试试看:

    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());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM