简体   繁体   中英

how to insert text in header using selenium webdriver

I have below code,

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

In the above code, i want to add extra text after "Hello there!" in the header

using selenium webdriver with java script. Please help.

I tried clicking on Hello there by locating it with xpath and trying to add text using sendKeys. I dont see any text added and code does not throw any error. I am trying to edit a html file on a online editor using selenium

Try executing JS in the browser using executeScript() function. You'll have to write a JavaScript code which will append/replace the text.

I'm not familiar with Selenium with JS but this a rough code which you can try out.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM