selenium web驱动程序sendKeys循环java

时间:2017-03-17 10:13:15

标签: java selenium-webdriver sendkeys

我应该如何在循环中使用sendKeys?第一次sendKeys工作正确,但第二次,在新页面上 - 例外。

public class main {
        public static void main(String args[]) throws Exception{
            System.setProperty("webdriver.gecko.driver", "C:\\Users\\asdasd\\Desktop\\geckodriver.exe");
            WebDriver driver = new FirefoxDriver();
            driver.get("http://www.kommersant.ru");
            Scanner scanner  = new Scanner(System.in);
            while(true) {
                WebElement search = driver.findElement(By.cssSelector(".search__input"));
               // WebElement searchButton = driver.findElement(By.cssSelector(".search__button"));
                String s = scanner.nextLine();

                if(s.equals("exit")){
                    break;
                }
                else {
                    WebDriverWait wait = new WebDriverWait(driver, 10);
                    search.sendKeys(s);
                    WebElement searchButton = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector(".search__button")));
                    searchButton.click();
                    searchButton.click();
                }

            }
            driver.quit();
        }
    }

First query hockey work correctly Second query footbal error

1 个答案:

答案 0 :(得分:0)

尝试让你的循环条件正确。问题就在那里。