【问题标题】:Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element [closed]线程“主”org.openqa.selenium.NoSuchElementException 中的异常:无法找到元素 [关闭]
【发布时间】:2016-08-03 09:31:13
【问题描述】:

我正在尝试使用 Selenium Webdriver 和 Java 来识别元素(文本框)。但是,它给了我无法找到元素异常。元素的 id 不变。这是 HTML 代码:

<div>
    <table id="searchIdText">
        <tbody>
            <tr id="searchIdText-inputRow">
                <td id="searchIdText-labelCell"><label
                    id="searchIdText-labelEl"> Search By Id <span
                        role="separator">:</span>
                </label></td>
                <td id="searchIdText-bodyEl"><input id="searchIdText-inputEl"
                    type="text" autocomplete="off" name="searchIdText-inputEl" size="1"
                    role="textbox" style="width: 100%; height: 20px;" /></td>
            </tr>
        </tbody>
    </table>
</div>

我想要的元素是 ID 为 searchIdText-inputEl 的文本框。我试过了:

driver.findElement(By.xpath(".//*[@id='searchIdText-inputEl']"));

driver.findElement(By.name("searchIdText-inputEl"))` 

还有:

driver.findElement(By.id("searchIdText-inputEl"));

但它们都不起作用。我也试过等待和Thread.sleep。他们都没有工作。此外,更改 HTML 代码在这里不是一个选项。欢迎所有除此之外的建议。谢谢!

Java 代码 sn-p 如下。作为一种绝望的措施,我将 Explicit Wait 和 Thread.sleep 添加在一起,因为它们都未能单独给我结果。但是,还是没用。

WebDriver driver = new FirefoxDriver();
WebElement element = null;
WebDriverWait wait = new WebDriverWait(driver, 30);
driver.get(url);
Thread.sleep(60000);
element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[@id='searchIdText-inputEl']"));
element.sendKeys("ABC");

【问题讨论】:

  • 页面加载后元素是否可见?您是否可以发布您当前的 Java 代码只是为了提供更多上下文?
  • 编辑并添加了Java代码sn-p。
  • 谢谢,你能看到页面加载,元素可见吗?
  • 是的。该元素是可见的,并且页面会完全加载。我试图打印页面标题只是为了确保驱动程序句柄位于正确的选项卡中。即使那是正确打印的。
  • 该元素不在页面的单独框架中是吗?

标签: java selenium xpath


【解决方案1】:

原来,有一个 iFrame,它被隐藏了,并且在页面加载时控件以某种方式转移到了该 iFrame。我切换了它,现在它可以正常工作了。

【讨论】:

    猜你喜欢
    • 2018-02-22
    • 2019-05-11
    • 2019-08-18
    • 1970-01-01
    • 2017-05-18
    • 1970-01-01
    • 1970-01-01
    • 2014-03-20
    • 2016-06-21
    相关资源
    最近更新 更多