【问题标题】:Cannot locate an element on page driver.findElement(By.id("Passwd"))在页面 driver.findElement(By.id("Passwd")) 上找不到元素
【发布时间】:2015-08-19 19:37:23
【问题描述】:

步骤:

  1. 转到 gmail.com - 好的

2.填写有效邮箱-OK

3按下一步 - 确定

4 通过id="Passwd" FAIL 查找元素

由于有效的输入字段id= "Passwd",测试失败。 但是我检查了 gmail.com 并且该字段具有 id="Passwd" 和 Name="Passwd"。我尝试使用 CSS 定位器并再次测试失败。

我试图在单击“下一步”和在字段中填写密码之间设置一个时间间隔。没用。

可能是因为当您将电子邮件放入字段并单击“下一步”时,会有一些脚本将“电子邮件输入表单”转换为“密码输入表单” 我该如何解决这个问题?

import org.junit.After;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;


public class GmailSIgnInTest {
    WebDriver driver = new FirefoxDriver();
    @Test
    public void loginShouldBeSuccessfull() {
        driver.get("http://gmail.com");
       WebElement userNameTextBox = driver.findElement(By.id("Email"));
        userNameTextBox.clear();
        userNameTextBox.sendKeys("MY EMAIL HERE");
        WebElement userNextButtonClick = driver.findElement(By.id("next"));
        userNextButtonClick.click();

        WebElement userPasswordTextBox = driver.findElement(By.id("Passwd")); // TEST FAIL
        userPasswordTextBox.clear();
        userPasswordTextBox.sendKeys("MY PASSWORD HERE")

;

【问题讨论】:

    标签: java selenium testing automation qa


    【解决方案1】:

    你可以试试 XPath:

    WebElement userPasswordTextBox = driver.findElement(By.Xpath("//*[@id="Passwd"]"));
    

    【讨论】:

      【解决方案2】:

      您需要等到元素出现在页面上。

      查看this

          WebDriverWait wait = new WebDriverWait(webDriver, 5);
          By passwordLocator = By.id("Passwd");
          wait.until(ExpectedConditions.elementToBeClickable(passwordLocator));
      
          WebElement userPasswordTextBox = webDriver.findElement(passwordLocator);
          userPasswordTextBox.clear();
          userPasswordTextBox.sendKeys("MY PASSWORD HERE");
      

      【讨论】:

        【解决方案3】:

        等待元素加载是这里的关键......

        试试这个

        1. 等待密码页面上提供指定的定位器。
        2. 按名称或 ID 查找定位器

        WebElement myDynamicElement = (new WebDriverWait(driver, 10)) .until(ExpectedConditions.presenceOfElementLocated(By.name("Passwd")));

        WebElement userPasswordTextBox = driver.findElement(By.name("Passwd"));

            Or
        

        WebElement userPasswordTextBox = driver.findElement(By.id("Passwd"));

        它对我有用。

        【讨论】:

          【解决方案4】:

          在调用 findElement 之前尝试刷新 html:

          webDriver.switchTo().DefaultContent();
          

          如果它没有帮助尝试: 单击按钮后,您可以访问其他 html 源和不同的框架 使用框架签入您的元素是否存在,如果框架不同,请尝试:

          webDriver.switchTo().frame("<FRAME NAME OR ID>");
          

          【讨论】:

            【解决方案5】:

            用 xPath 试试这个

            WebElement userPasswordTextBox = driver.findElement(By.Xpath("// [@id="Passwd"]"));
            

            【讨论】:

              猜你喜欢
              • 2021-10-17
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2018-12-09
              • 2012-06-25
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多