【问题标题】:Selenium Web Driver FindElement() method doesnt work(NoSuchElementException)Selenium Webdriver FindElement() 方法不起作用(NoSuchElementException)
【发布时间】:2018-07-17 23:03:03
【问题描述】:

所以我的问题很简单。这是我的代码:

    driver.findElement(By.id("j_username")).sendKeys("nk");
    driver.switchTo().frame(0);
    driver.findElement(By.id("j_password")).sendKeys("1");

第一个 findElement() 方法可以正常工作,但是当我第二次尝试使用它时,我得到了这个错误:

org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"id","selector":"j_password"}

所有的 id 都是正确的。

【问题讨论】:

  • 为什么要切换到框架?是这个框架中的第二个元素吗?
  • 你移动到一个框架。在驱动程序被移动的框架中,是否存在您尝试查找的元素?
  • @AndreiSuvorkov 不,不是。我刚刚添加了这个,因为有人告诉我这会有所帮助,但没有帮助:)
  • 然后删除它:)。并检查它是否有效。如果没有,请添加指向网站或相关 HTML 块的链接
  • @AndreiSuvorkov 删除没有帮助。这是HTML代码<tbody><tr><td>Логин:</td><td><input type="text" name="j_username" id="j_username" autocorrect="off" autocapitalize="off"></td></tr><tr><td>Пароль:</td><td><input type="password" name="j_password"></td></tr><tr><td align="right"><input id="remember_me" type="checkbox" name="remember_me"></td><td><label for="remember_me">Запомнить меня</label></td></tr></tbody>

标签: java selenium selenium-webdriver


【解决方案1】:

试试这个:

driver.findElement(By.id("j_username")).sendKeys("nk");
driver.findElement(By.name("j_password")).sendKeys("1"); // find by name

第二个元素没有id,至少在你提供的HTML中。

【讨论】:

  • P.S.:您需要确保没有其他元素带有 attribute name j_password
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多