Home > Back-end >  Selenium VBA using JS path to send text to web element
Selenium VBA using JS path to send text to web element

Time:11-23

Using Selenium VBA Code using "JS PATH".

I have a problem with only this specific HTML, because it is structured in a way that makes entering text harder.

The HTML:
<textarea class="messenger-composer" placeholder="Say something…" style="height: 34px;">THE TEXT NEEDS TO GO HERE </textarea>

  ''Code1 with querySelector returns error ")" is missing:
    Dim Textw As String
    cScript = "document.querySelector('textarea[placeholder='Say something…']').innerHTML = '" & Textw & "'"
    bot.ExecuteScript cScript


  ''  Code2 with querySelector pastes the text in the wrong location making the HTML page corrupt.
      Dim Textw As String
      cScript = "document.querySelector('#main_content > div.notifications-footer.notifications-footer--collapsed > div.messenger > div.FullscreenOverlay-content > div > div > div > div > div > div > div.messenger-composer-wrapper ').innerHTML ='" & Textw & "'"
      bot.ExecuteScript cScript

enter image description here

enter image description here

CodePudding user response:

Dim Textw As String
    cScript = "document.getElementsByClassName('messenger-composer')[0].innerHTML = '" & Textw & "'"
    bot.ExecuteScript cScript
''send key is disabled, this workaround makes it enabled.
        bot.SendKeys "a"
        bot.SendKeys bot.Keys.Backspace
  • Related