Search code examples
pythonseleniumcodemirror

How do I edit CodeMirror with Selenium in Python?


I keep getting the error message below every time I try to insert text into CodeMirror on my webpage. Does anyone know how to successfully edit codemirror with selenium?

WebDriverException: Message: unknown error: Cannot read property 'setValue' of undefined

This is my Selenium-Python code

def click_component_script_editor(self):
   driver = self.driver
   line18Edit = self.driver.find_element(By.XPATH, "//html//div[@class='CodeMirror-line']//div[18]/pre[1]")      
   driver.execute_script("arguments[0].CodeMirror.setValue(arguments[1]);",
                   line18Edit,
                   "foo.bar")

Solution

  • Figured out the answer to this one, I had to use actionChains instead of just regular old send_keys.

    codeMirror = self.driver.find_element(".CodeMirror")
    action_chains.click(codeMirror).perform()
    action_chains.send_keys("Hello World").perform()