Skip to main content
 首页 » 编程设计

python之如何在 Python 中使用 Selenium 编辑 CodeMirror

2025年01月19日8unruledboy

每次我尝试将文本插入网页上的 CodeMirror 时,我都会不断收到以下错误消息。有谁知道如何使用 selenium 成功编辑 codemirror?

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

这是我的 Selenium-Python 代码

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

请您参考如下方法:

想出了这个问题的答案,我不得不使用 actionChains 而不是常规的旧 send_keys。

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