Selenium Webdriver:有时会将密钥发送到错误的字段

时间:2016-01-12 10:01:26

标签: java selenium selenium-webdriver

请参阅以下代码 在这里,我试图自动化Jira的报告。

步骤:

1) Load Jira homepage
2) Wait for page to load
3) Add username to username field
4) Add password to password field
5) ...

在执行第4步时,有时它工作正常,有时它会为用户名字段本身添加密码,并将密码字段留空。

请帮我确定问题。

 public static void main(String[] args) throws ParserConfigurationException {
            WebDriver driver = new FirefoxDriver();
    // jiraHomePage = home page url (string)
            driver.get(jiraHomePage);
            driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
            WebDriverWait wait = new WebDriverWait(driver, 20);
            wait.until(ExpectedConditions.elementToBeClickable(By.id("gadget-0"))); 
            driver.switchTo().frame("gadget-0");

            WebElement userNameField = driver.findElement(By
                    .id("login-form-username"));
            userNameField.sendKeys(username);
            WebElement passwordField = driver.findElement(By
                    .id("login-form-password"));
// this sometimes sends keys to username field instead of password field
            passwordField.sendKeys(password);
            WebElement loginButton = driver.findElement(By.id("login"));
            loginButton.click();
            driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
            WebDriverWait wait2 = new WebDriverWait(driver, 20);
            wait2.until(ExpectedConditions.elementToBeClickable(By.id("gadget-10002-title"))); 
            // source is page of report which I want to go to after login
            driver.get(source);

            DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
            InputSource is = new InputSource();
            is.setCharacterStream(new StringReader(driver.getPageSource()));
            Reporter.createReport(is,args[0]);
             driver.close();
        }

1 个答案:

答案 0 :(得分:-1)

在这种情况下,用户名和密码之间有一些等待。我希望

 Thread.sleep(2000);

在这里帮助更好。

由于