2015-11-05 118 views
0

我有下面的代码中插入页眉文字,如何使用硒的webdriver

<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。请帮忙。

我试着用xpath定位它并尝试使用sendKeys添加文本,然后单击Hello。我没有看到任何文本添加和代码不会引发任何错误。 我想在使用硒的在线编辑器上编辑html文件

+0

请分享您使用硒写入发送文本的代码。你也可以只在文本框,富文本框中插入文本。你正试图插入h1标签?似乎不可能。 –

+0

你确定那里的文字是可编辑的吗?它看起来不像来自您提供的HTML。 – JeffC

回答

0

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

我不熟悉Selenium和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); 
});  
+0

我试着添加代码并运行脚本:我得到下面的错误: script =“theParent = document.getElementByTagName('body'); ^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ [启动程序]错误:SyntaxError:意外的令牌ILLEGAL – user5462020

+0

@ user5462020 - I'对不起,对JS不熟悉Selenium,但代码看起来有点类似于我所做的。 – JRodDynamite