2017-03-28 46 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

你能发送返回键,而不是点击登录按钮?

相关问题