首页 > 解决方案 > Selenium 找不到可见的元素

问题描述

这是我尝试查找选定输入的 HTML

HTML

我尝试像这样发送key()到这个输入

String xPath = "//*[@id='id_username']";

WebDriverWait wait = new WebDriverWait(driver, 30);

wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(xPath))).sendKeys("text");

总是得到这个错误org.openqa.selenium.TimeoutException。通常,当元素在设定的时间内不可见时,我会收到此错误。

整个 html 中没有 iframe。

可能是哪个原因?

标签: javaselenium

解决方案


您需要考虑以下几点:

  • 而不是String尝试将xpath定义为By.
  • sendKeys()在您调用而不是ExpectedConditions方法visibilityOfElementLocated()使用elementToBeClickable()方法时继续前进。
  • 由于该元素是<input>尝试构建粒度xpath
  • 您的代码块将如下所示:

    By xPath = By.xpath("//form[@action='/accounts/register/']/fieldset[@class='fieldset_main']//input[@id='id_username' and @name='username']");
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(xPath)).sendKeys("text");
    

推荐阅读