【问题标题】:how to insert text in header using selenium webdriver如何使用 selenium webdriver 在标题中插入文本
【发布时间】:2015-11-06 12:46:39
【问题描述】:

我有以下代码,

<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>

在上面的代码中,我想在“Hello there!”之后添加额外的文本。在标题中

使用 selenium webdriver 和 java 脚本。请帮忙。

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

【问题讨论】:

  • 请分享您为使用 selenium 发送文本而编写的代码。您也可以仅在文本框、富文本框中插入文本。你想插入h1标签吗?似乎不可能。
  • 你确定那里的文字是可编辑的吗?它看起来不像来自您提供的 HTML。

标签: javascript selenium selenium-webdriver


【解决方案1】:

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

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

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);
});    

【讨论】:

  • 我尝试添加代码并运行脚本:我得到以下错误: script = "theParent = document.getElementByTagName('body'); ^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ [启动器] 错误:语法错误:意外令牌非法跨度>
  • @user5462020 - 对不起,不熟悉 Selenium 和 JS,但代码看起来与我所做的有点相似。
猜你喜欢
  • 2016-04-01
  • 2013-05-05
  • 1970-01-01
  • 2014-06-29
  • 2021-06-09
  • 1970-01-01
  • 1970-01-01
  • 2018-01-03
  • 1970-01-01
相关资源
最近更新 更多