2015-11-05 139 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