1

I am having a bit of a problem with Selenium. I am new to Selenium, web scraping and python as a whole. I am doing a practice project where I can send emails straight from my IDE.

I have been able to enter things into the 'send to', 'cc', and 'subject lines' well. I am having a problem entering body text.

Here is a screenshot of my email/inspect element:

Here is a screenshot of my email/inspect element

As you can see in this image (pixelized by stackOverflow but I think you can get the idea, the body is one big box with the ID 'tinymce'. The code block representing this text input is:

<body id="tinymce" class="mce-content-body mce-inactive-editor" data-id="ZmHtmlEditor1_body" 
contenteditable="true" style="font-family: arial, helvetica, sans-serif; font-size: 10pt; color: rgb(0, 
0, 0); width: 696px; height: 212px;" data-mce-style="font-family: arial, helvetica, sans-serif; font-
size: 10pt; color: #000000;" dir="LTR" aria-label="Compose body"><div><br data-mce-bogus="1"></div>
</body>

my code, to open the text box, is

# EMAIL BODY TEXT
content = pyit.inputStr(prompt='What is the body of your email?\n')

clickBodyText = browser.find_element_by_id('tinymce').click()
bodyText = browser.find_element_by_id('tinymce').sendKeys(content)

I click on the body text first to make the input active, and sendkeys of the content. This returns the no such element error

    selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="tinymce"]"}
  (Session info: chrome=84.0.4147.125)
DebanjanB
  • 118,661
  • 30
  • 168
  • 217

2 Answers2

-1

To send a character sequence within the element you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "body.mce-content-body.mce-inactive-editor#tinymce"))).sendKeys(content)
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//body[@class='mce-content-body mce-inactive-editor' and @id='tinymce']"))).sendKeys(content)
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
DebanjanB
  • 118,661
  • 30
  • 168
  • 217
-2

you can use

find_element_by_id()

or

find_element_by_xpath()

but the error will be same if the element is not loaded before the execution of the command for that you can use following mathod

import time
time.sleep(5)

use time.sleep after you open a webpage so it can be loaded fully before performing any operation

Parth B Thakkar
  • 105
  • 2
  • 9
  • I don't think this is the problem - my entire website was already loaded beforehand. I had a similar problem earlier on in the project which is used time.sleep() with, but the page is already loaded for this. – DownstairsPanda Aug 17 '20 at 03:20
  • 1
    if you want to insert anything in the input area you don't need to click on it you can simply send keys to the textarea element – Parth B Thakkar Aug 17 '20 at 03:28
  • that might be so but i still cannot find the element. – DownstairsPanda Aug 17 '20 at 23:11