vendredi 18 février 2022

Variable/Changing ember number in Intercom HTML. Selenium Python

I am trying to automate some tasks at work. Requests wont work because I don't have admin access to my works Intercom App. Therefore I use Selenium.

I want to write "Hey" in the chat box of Intercom, and send the message.

** The problem is a changing ember number every time I have a new conversation. It works when I copy the right ember number every time, but when changing the conversation, it doesn't work anymore. **

I am looking for some kind of script to change the ember = XXXXX into the right number each time

Not really relevant to the code problem, but I am using Chrome in debugging mode, to avoid logging in every time I need to test the code, and I am using tkinter to have a button to press, every time I want to write "Hey" in the chat box.

Sorry, I understand it is difficult to replicate this problem.

from selenium import webdriver
from selenium.webdriver.chrome.options import Options 

#___________
#In order to run Selenium in an already opened browser / session, I need to run this code in CMD:
#cd C:\Program Files (x86)\Google\Chrome\Application
#
#chrome.exe --remote-debugging-port=9222 --user-data-dir="C:\Users\peter\testprogram"
#___________

opt=Options()
opt.add_experimental_option("debuggerAddress","localhost:9222")
driver=webdriver.Chrome(executable_path="
C:\\ProgramFiles\\crromedriver\\chromedriver.exe",options=opt)

def hey():

    ember = 32890
    hey = driver.find_element_by_xpath('//*[@id="ember'+str(ember)+'"]/div/div[3]/div[1]/div/p')

    hey.send_keys("Hey!")

The specific HTML element where I want to write "Hey!": (This is under the big HTML code below)

<p class="intercom-interblocks-align-left embercom-prosemirror-composer-block-selected" style="">Hey! This is where I want my text</p>

One might suggest to use

hey = driver.find_element_by_class_name('intercom-interblocks-align-left embercom-prosemirror-composer-block-selected')
hey.send_keys("Hey!")

But this doesn't work for me.

The HTML element where the ember number is changing:

<div id="ember32890" class="u__relative inbox__conversation-composer__wrapper ember-view"><div>
    <div></div>
      <div>        
      
</div>

<div data-test-prosemirror-composer="" class="composer-inbox composer-style-basic o__fit conversation__text embercom-prosemirror-composer ">
    <style>
  .ProseMirror {
    outline: none;
    white-space: pre-wrap;
  }

  .ProseMirror .intercom-interblocks-html-block {
    white-space: normal;
  }

  li.ProseMirror-selectednode {
    outline: none;
  }

  .ProseMirror-selectednode.embercom-prosemirror-composer-image img,
  .ProseMirror-selectednode.embercom-prosemirror-composer-video iframe,
  .ProseMirror-selectednode.embercom-prosemirror-composer-messenger-card
    .intercom-interblocks-messenger-card,
  .ProseMirror-selectednode.embercom-prosemirror-composer-html-block,
  .ProseMirror-selectednode.embercom-prosemirror-composer-button .intercom-h2b-button {
    outline: 2px solid #8cf;
  }

  hr.ProseMirror-selectednode,
  .embercom-prosemirror-composer-template.ProseMirror-selectednode,
  .embercom-prosemirror-composer-mention.ProseMirror-selectednode {
    outline: 1px solid #8cf;
  }
</style>
    <div>
<!----><div contenteditable="true" role="textbox" dir="auto" data-insertable="true" class="ProseMirror embercom-prosemirror-composer-editor dir-auto"><p class="intercom-interblocks-align-left embercom-prosemirror-composer-block-selected" style="">Hey!Hey!Hey!Hey!Hey!</p><p class="intercom-interblocks-align-left" style=""><br></p></div></div>
      <div class="flex flex-row flex-wrap gap-4 embercom-prosemirror-composer-attachment-list">
<!----></div>

      <!---->
      <!---->
      <!---->
      <!---->

      <!---->
      <div></div>
<!---->

<!----></div>
        <!---->

<!----></div></div>




Aucun commentaire:

Enregistrer un commentaire