Volatil3
Volatil3

Reputation: 14988

Python Selenium: How to set text of an DIV based editor?

I am trying to send Twitter DM via Python Selenium. I am able to select and click the "editor" but can't send keys. The code is given below:

import time

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException


def is_dm_enabled():
    driver.get('https://twitter.com/<TWITTER HANDLE YOU FOLLOW>')
    time.sleep(2)
    has_dm = False
    dm_elem = None

    try:
        dm_icon = driver.find_element_by_xpath(
            '//*[@id="react-root"]/div/div/div/main/div/div/div/div[1]/div/div[2]/div/div/div[1]/div/div[1]/div/div[2]')
        if dm_icon:
            has_dm = True
            dm_elem = dm_icon
    except NoSuchElementException:
        has_dm = False
        dm_elem = None
    finally:
        return has_dm, dm_elem


def login_twitter(user, pwd):
    USERNAME = user
    PASSWORD = pwd

    # navigate to the application home page
    driver.get("https://twitter.com/login")

    # get the username textbox
    login_field = driver.find_element_by_name("session[username_or_email]")
    login_field.clear()

    # enter username
    login_field.send_keys(USERNAME)
    time.sleep(1)

    # get the password textbox
    password_field = driver.find_element_by_name("session[password]")
    password_field.clear()

    # enter password
    password_field.send_keys(PASSWORD)
    time.sleep(1)
    password_field.submit()


if __name__ == '__main__':
    driver = webdriver.Firefox()
    driver.implicitly_wait(30)
    driver.maximize_window()

    login_twitter('<YOUR TWITTER USER>', '<YOUR TWITTER PASSWORD>')
    time.sleep(5)
    f, e = is_dm_enabled()
    if f:
        e.click()
        time.sleep(5)
        driver.find_element_by_class_name('DraftEditor-editorContainer').click()
        driver.execute_script("document.querySelector(\".DraftEditor-editorContainer span\").style.display = \"block\"")
        driver.execute_script("document.querySelector(\".DraftEditor-editorContainer span\").innerHTML = 'Take this'")
        # elems = driver.find_elements_by_css_selector('.DraftEditor-editorContainer')
        # print(len(elems))

Screenshot the DOM is given below:

enter image description here

Upvotes: 0

Views: 1364

Answers (2)

Gokul nath
Gokul nath

Reputation: 524

I have ran this code in Chrome with alternate suggested by Naveen,

driver.find_element_by_xpath("//*[contains(@class, 'DraftStyleDefault')]").click()
driver.find_element_by_xpath("//*[contains(@class, 'DraftStyleDefault')]").clear()
driver.find_element_by_xpath("//*[contains(@class, 'DraftStyleDefault')]").send_keys('Your Message here.')

It's working fine for me, i'm suspecting there may be a mismatch between your browser version and driver version. Try downloading the driver that supports your browser. I too had similar issues in past, it was fixed when i corrected my driver version that matches browser!

Upvotes: 1

Naveen
Naveen

Reputation: 788

Instead of executing JS to type in the UI, try sendKeys method.

use the XPATH of the actual input field : //*[contains(@class, 'DraftStyleDefault')]. So that would make your code like:

driver.find_element_by_xpath("//*[contains(@class, 'DraftStyleDefault')]").click()
driver.find_element_by_xpath("//*[contains(@class, 'DraftStyleDefault')]").clear()
driver.find_element_by_xpath("//*[contains(@class, 'DraftStyleDefault')]").send_keys('Your Message here.')

Upvotes: 1

Related Questions