2017-03-28 47 views
1

我正在嘗試爲Gmail構建Selenium自動化框架。 我已經安裝了以下工具: JDK,Eclipse,Selenium Jars,Gradle,TestNG在Selenium Webdriver中,在文本框中輸入文本後單擊按鈕

我正在嘗試登錄到gmail。但是,當我輸入用戶名時,我得到了低於錯誤的信息。 在輸入用戶名之前,它試圖點擊「下一步」按鈕。

我可以在開發框架時使用wait嗎? 在撥打wait時是否需要維護任何標準? 寫下任何用戶定義的wait方法。

錯誤: 失敗:gmailLoginShouldBeSuccessful org.openqa.selenium.ElementNotVisibleException:不能點擊元素(警告:服務器未提供任何信息棧跟蹤) 命令持續時間或超時:207毫秒

我的代碼:

@Test 
public void gmailLoginShouldBeSuccessful(){ 
    //1.Go to Gmail website 
    System.setProperty("webdriver.ie.driver", "C:\\Selenium_Softwares_Docs_Videos\\IEDriverServer_x64_3.1.0\\IEDriverServer.exe"); 
    WebDriver driver = new InternetExplorerDriver(); 
    driver.manage().deleteAllCookies(); 
    driver.manage().window().maximize(); 
    driver.get("http://gmail.com");  
    //2.Fill in username 
    WebElement userTextBox = driver.findElement(By.id("Email")); 
    userTextBox.clear(); 
    userTextBox.sendKeys("xxxx"); 
    //3. click on next button 
    WebElement nxtBtn = driver.findElement(By.id("next")); 
    nxtBtn.click(); 
    //4.Fill in password 
    WebElement pwdTextBox = driver.findElement(By.id("Passwd-hidden")); 
    userTextBox.clear(); 
    userTextBox.sendKeys("xxxxxxx"); 
    //5.Click sign in 
    WebElement signBtn = driver.findElement(By.id("signIn")); 
    signBtn.click();   
} 
+0

你應該使用pwdTextBox代替userTextBox設置密碼 – kushal

+0

嗨,等待調整已經在WATIR(Selenium wrapper)中做得非常好,它檢查是否存在?,可見?,啓用?寫嗎?在它與任何元素交互之前,如果它延遲這四個中的任何一個,它就等待。嘗試一下。 – RAJ

+1

有一個gmail的API。爲什麼不使用它而不是自動化UI? – JeffC

回答

0

您可以使用顯式等待來實現您的要求。

WebDriverWait wait = new WebDriverWait(yourWebDriver, 5); 
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//xpath_to_element"))); 

Webdriver會等待5秒讓您的元素能夠被點擊。

+0

這可能沒有解決問題。可點擊的條件可以滿足,但用戶名尚未輸入。 – gm2008

0

使用Chrome瀏覽器,而不是驅動程序的Internet Explorer:

import java.util.concurrent.TimeUnit; 

import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.chrome.ChromeDriver; 
import org.testng.annotations.Test; 

public class TestCommons { 
    @Test 
    public void gmailLoginShouldBeSuccessful() throws InterruptedException { 
     // 1.Go to Gmail website 
     System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "\\chromedriver.exe"); 
     WebDriver driver = new ChromeDriver(); 
     driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 
     driver.manage().window().maximize(); 
     driver.get("http://gmail.com"); 
     // 2.Fill in username 
     driver.findElement(By.id("Email")).clear(); 
     driver.findElement(By.id("Email")).sendKeys("vishala"); 
     // 3. click on next button 
     driver.findElement(By.id("next")).click(); 
     // 4.Fill in password 
     driver.findElement(By.id("Passwd")).sendKeys("vishala"); 
     // 5.Click sign in 
     driver.findElement(By.id("signIn")).click(); 
     driver.quit(); 
    } 
} 

希望這會爲你:)工作

0

你能發送返回鍵,而不是點擊登錄按鈕?

相關問題