如何使用seleniumwebdriver在页眉中插入文本

how to insert text in header using selenium webdriver

本文关键字:插入文本 何使用 seleniumwebdriver      更新时间:2023-09-26

我有下面的代码,

<body>
    <table>
      <tr>
        <td style= "width:30%;">
          <img class = "new" src="images/new-icon.png">
        <td>
            <h1>Hello there!</h1>
          <p>Thanks for logging in
            <span class = "blue">image edit application</span>.
            Get started by reading our
            <a href = "https:abc">documentation</a>
            or use the xxxxxxxxxxxx.
    </table>
  </body>

在上面的代码中,我想使用带有java脚本的selenium webdriver在

标题中的"Hello there!"之后添加额外的文本。请帮忙。

我试着点击Hello,用xpath定位它,并尝试用sendKeys添加文本。我没有看到任何添加的文本,代码也没有抛出任何错误。我正在尝试使用selenium 在在线编辑器上编辑html文件

尝试使用executeScript()函数在浏览器中执行JS。您必须编写一个JavaScript代码来附加/替换文本。

我不熟悉Selenium with JS,但这是一个可以尝试的粗略代码。

script = "theParent = document.getElementByTagName('body');
theKid = document.createElement('div');
theKid.innerHTML = 'Hello There!';
// append theKid to the end of theParent
theParent.appendChild(theKid);
// prepend theKid to the beginning of theParent
theParent.insertBefore(theKid, theParent.firstChild);"
driver.executeScript('return ' + script).then(function(return_value) { 
    console.log('returned ', return_value);
});