1

I am trying to select a text box which is in the form of a <!DOCTYPE html>

enter image description here

Here's the HTML:

enter image description here

I tried pretty much everything and either I end up with nothing. Here's my code

desc =WebDriverWait(browser,30).until(EC.visibility_of_element_located((By.XPATH, '//*[@id="tinymce"]')))
desc.clear()
desc.send_keys("anynaynanya")

If I used the xpath of the iframe I get that error:

    Traceback (most recent call last):
  File "C:\Users\Dln\Desktop\tstsd.py", line 22, in <module>
    desc.clear()
  File "C:\Users\Dln\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\remote\webelement.py", line 95, in clear
    self._execute(Command.CLEAR_ELEMENT)
  File "C:\Users\Dln\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\remote\webelement.py", line 633, in _execute
    return self._parent.execute(command, params)
  File "C:\Users\Dln\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Users\Dln\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidElementStateException: Message: invalid element state
  (Session info: chrome=85.0.4183.83)
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Amr
  • 119
  • 1
  • 10

3 Answers3

2

Try switching to frame first

To switch to the frame with frame name

driver.switch_to.frame("framename")

check out this link https://www.tutorialspoint.com/how-to-handle-frames-in-selenium-with-python

1

To access any frame you first need to switch to it. Just to make sure that there is frame on frame on webpage, you may find the frames by tag name iframe

driver.find_elements_by_tag_name("iframe")

A list of frames available will be given. This is just to be more sure about the presence of frame. To send some text inside you need to switch to frame, that you can do by providing the index well as frame name.

driver.switch_to.frame()

Note: Index no. starts from 0

0

The <p> element is within an <iframe> so you have to:

  • Induce WebDriverWait for the desired frame to be available and switch to it.

  • Induce WebDriverWait for the desired element to be clickable.

  • You can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    driver.get('https://www.t-online.de/themen/e-mail')
    WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe#textarea-WYSIWYG_ifr")))
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "body.mce-content-body#tinymce > p"))).send_keys("anynaynanya")
    
  • Using XPATH:

    driver.get('https://www.t-online.de/themen/e-mail')
    WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@id='textarea-WYSIWYG_ifr']")))
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//body[@id='tinymce' and @class='mce-content-body']/p"))).send_keys("anynaynanya")
    
  • 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
    

Reference

You can find a couple of relevant discussions in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352