How to replace *parts* of a text in a text box

The problem: If you use type | locator | text here then the complete text in a textbox gets replaced. But what if you want to change only a part of the content, e. g. a link?

Example: Replace a string in the html code here: https://html-css-js.com/html/

Solution:

  1. Extract (web scrape) the text from the box with store Value

  2. Replace parts of the string with the Javascript Replace function and Execute Script

  3. Refill the text box with the new string with TYPE

Demo macro:

Here we replace inside the HTML the link

Source code:

{
  "Name": "Replace Partial Text",
  "CreationDate": "2019-12-6",
  "Commands": [
    {
      "Command": "open",
      "Target": "https://html-css-js.com/html/",
      "Value": ""
    },
    {
      "Command": "type",
      "Target": "xpath=//*[@id=\"sourceFieldecske\"]/div/div[1]/textarea",
      "Value": "<iframe allowfullscreen=\"true\" autoplay=\"off\" height=\"350\" mozallowfullscreen=\"true\" name=\"iframe1\" src=\"https://img4.hostingpics.net/pics/tidj.jpg\" webkitallowfullscreen=\"true\" width=\"100%\"></iframe><style>a.link-iframe{display: inline-block;padding: 20px;background: #222;margin : 10px;color: #f9813f;font-family: inherit;font-size: 14px;font-weight: 700; }   "
    },
    {
      "Command": "comment",
      "Target": "Demo: How to edit this text",
      "Value": ""
    },
    {
      "Command": "comment",
      "Target": "First extract the text",
      "Value": ""
    },
    {
      "Command": "storeValue",
      "Target": "xpath=//*[@id=\"sourceFieldecske\"]/div/div[1]/textarea",
      "Value": "textold"
    },
    {
      "Command": "comment",
      "Target": "Then search and replace the string",
      "Value": ""
    },
    {
      "Command": "executeScript_Sandbox",
      "Target": "var str = ${textold}; var res = str.replace(\"https://img4.hostingpics.net/pics/tidj.jpg\", \"https://newlink.newdomain.net/pics/NEW.jpg\"); return res;",
      "Value": "textnew"
    },
    {
      "Command": "comment",
      "Target": "Now fill in the new string, overwriting the old content",
      "Value": ""
    },
    {
      "Command": "type",
      "Target": "xpath=//*[@id=\"sourceFieldecske\"]/div/div[1]/textarea",
      "Value": "${textnew}"
    }
  ]
}