【问题标题】:Why Gmail Password field couldnt be sent keys with "sendkeys" in Selenium Webdriver (Java)?为什么无法在 Selenium Webdriver (Java) 中使用“sendkeys”发送 Gmail 密码字段?
【发布时间】:2017-03-17 23:49:36
【问题描述】:

在代码下方运行时出现“元素不可见”错误。

当我通过谷歌检查并进行一些调查时,发现密码带有“密码隐藏”属性。我该如何摆脱它。

driver.get("http://www.gmail.com");
driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
try{
driver.findElement(By.xpath("//input[@id='Email']")).sendKeys("ashwinxxxxx@gmail.com");
        driver.findElement(By.xpath("//input[@id='next']")).click();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
 driver.findElement(By.xpath("//div[2]/div/div/input[2]")).sendKeys("abcd2123");
        driver.findElement(By.xpath("//input[@id='signIn']")).click();
    }
    catch (Throwable e)
    {
        System.out.println(e.getMessage());    }
 }

【问题讨论】:

  • 你能把 XPath 表达式改成 "//*[@id='Passwd']" 吗?
  • 没有。没有运气添加任何元素查找器。

标签: javascript java selenium selenium-webdriver


【解决方案1】:

尝试使用 id 查找元素,如下所示:

WebElement element1 = driver.findElement(By.id("Passwd"));
element1.sendKeys("Password");

如果失败,发布收到的异常或错误。

WebDriverWait wait = new WebDriverWait(driver, 15);
WebElement element1 = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("Passwd")));
element1.sendKeys("Password");

【讨论】:

  • 与以前相同的异常 - “元素不可见”
  • 尝试使用显式等待。我已根据需要修改了答案。
  • 我已经清楚地提到了上面的答案以及所有细节。 2天不能接受我的回答。你能做吗
【解决方案2】:

以下显式等待有效。

WebDriverWait wait = new WebDriverWait(driver,30);
        WebElement element1 = wait.until(
                ExpectedConditions.elementToBeClickable(By.id("Passwd")));
        element1.sendKeys("xxxxxx");

要导入的类是:

导入 org.openqa.selenium.support.ui.ExpectedConditions; 导入 org.openqa.selenium.support.ui.WebDriverWait;

【讨论】:

    【解决方案3】:

    我使用 WebDriver Wait 并将定位器从 'id' 更改为 'xpath'

    WebDriverWait wait = new WebDriverWait(driver, 10);
    WebElement MyPass = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[@id='password']/div[1]/div/div[1]/input")));
    MyPass.sendKeys("Password");
    

    【讨论】:

      猜你喜欢
      • 2013-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-21
      • 1970-01-01
      • 2017-11-28
      相关资源
      最近更新 更多