3

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")

1 Answer 1

4

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()
Sign up to request clarification or add additional context in comments.

5 Comments

how about send_keys to partical line of code in codeMirror?
That causes an "unable to focus element error" every single time.
well, it turn out i use wait.until to make sure next line of codemirror element is visibility and repeat to click element and send_keys. ui.WebDriverWait(driver, timeout=30).(expected_conditions.visibility_of_element_located(By.css, 'CodeMirror'))
What is action_chains here? from selenium.webdriver.common import action_chains leads to the error AttributeError: module 'selenium.webdriver.common.action_chains' has no attribute 'click'
@Dan The problem is that you're trying to use driver.find_element[s] instead of driver.find_element. So one s makes difference. If you want to find single element, you should try like this: Use this way: driver.find_element_by_link_text(".CodeMirror") or you want to find whole list, Use like this: for x in self.driver.find_elements_by_link_text(".CodeMirror"):\n link =webdriver.ActionChains(self.driver).move_to_element(x).click(x).perform()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.