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 Stack Overflow, 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)
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131

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 a 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 an XPath selector:

    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
    
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
-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 the following method:

import time

time.sleep(5)

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Parth B Thakkar
  • 115
  • 2
  • 10
  • 1
    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