【问题标题】:How to append "Input Tags" into the DOM in Runtime Dynamically using Selenium JavascriptExecutor?如何使用 Selenium JavascriptExecutor 在运行时动态地将“输入标签”附加到 DOM 中?
【发布时间】:2015-08-16 05:52:50
【问题描述】:

我需要在运行时(动态)将以下元素附加到 DOM..

    <input type="text" name="imagesToAdd" value="3566">

我尝试使用 Selenium JavascriptExecutor 来实现;但它给出了一条错误消息“org.openqa.selenium.WebDriverException: document.getElementById(...).append is not a function”

((JavascriptExecutor) driver).executeAsyncScript("document.getElementById('post-ad_title').append('<input type=\"text\" name=\"imagesToAdd\"value=\"3566\">')");

【问题讨论】:

    标签: java javascript selenium executor


    【解决方案1】:

    您可以通过使用Node.appendChild() 并设置所需的属性来完成此操作;

    String script = "var p = document.createElement('input');var ele = document.getElementById('post-ad_title');p.setAttribute('type','text');p.setAttribute('name','imageToAdd');p.setAttribute('value','3566'); ele.appendChild(p);";
    ((JavascriptExecutor) driver).executeScript(script);
    

    【讨论】:

    • 这工作正常,但在页面末尾添加了按钮,我怎样才能将 属性注入特定的 div ?
    • 这是我的 div:
      跨度>
    猜你喜欢
    • 1970-01-01
    • 2019-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-29
    • 1970-01-01
    • 2014-06-28
    • 2020-03-15
    相关资源
    最近更新 更多