【问题标题】:In Selenium Webdriver, click button once the text is entered in text box在 Selenium Webdriver 中,在文本框中输入文本后单击按钮
【发布时间】:2017-03-28 12:54:45
【问题描述】:

我正在尝试为 Gmail 构建 Selenium 自动化框架。 我已经安装了以下工具: JDK、Eclipse、Selenium 罐子、Gradle、TestNG

我正在尝试登录 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();        
}

【问题讨论】:

  • 设置密码时应使用 pwdTextBox 而不是 userTextBox
  • 嗨,等待调整在 WATIR(Selenium 包装器)中做得非常好,它检查是否存在?,可见?,启用?可写?在它与任何元素交互之前,如果它延迟了这四个元素中的任何一个,它就会等待。试试看。
  • 有一个用于 gmail 的 API。为什么不使用它而不是自动化 UI?
  • 输入文字后可以使用Thread.sleep()。如果这可行,那么继续进行一些显式等待()和元素的可见性。

标签: java selenium


【解决方案1】:

您可以使用显式等待来满足您的要求。

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

Webdriver 将等待 5 秒,以便您的元素能够被点击。

【讨论】:

  • 这可能没有解决问题。可能满足可点击条件,但尚未输入用户名。
【解决方案2】:

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

希望这对你有用:)

【讨论】:

    【解决方案3】:

    您能否发送一个 RETURN 键而不是单击登录按钮?

    【讨论】:

      猜你喜欢
      • 2021-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-24
      • 2013-10-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多