pythonselenium-webdriverxpathwebdriverwaitcontenteditable

How to write text into a non-input element using Selenium Python


I am trying to send automated google chats but the element of the text box in which I write the message has no <input> tag. The <div>s are not taking input. Here is the structure of the chat box:

<div jsname="yrriRe" jsaction="touchend:ufphYd; input:q3884e; paste:QD1hyc; drop:HZC9qb;" class="T2Ybvb KRoqRc editable" role="textbox" aria-label="History is on" aria-multiline="true" spellcheck="true" id="T2Ybvb0" g_editable="true" contenteditable="true"><br></div>

I tried this but is not working

element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[@id='T2Ybvb0']"))).send_keys("GOOD MORNING")

Solution

  • Given the HTML:

    <div jsname="yrriRe" jsaction="touchend:ufphYd; input:q3884e; paste:QD1hyc; drop:HZC9qb;" class="T2Ybvb KRoqRc editable" role="textbox" aria-label="History is on" aria-multiline="true" spellcheck="true" id="T2Ybvb0" g_editable="true" contenteditable="true">
        <br>
    </div>
    

    The <div> element is a dynamic element and contains the attribute contenteditable="true". So ideally, to send a character sequence to the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies: