如何在CodeMirror Edi中使用selenium模拟关键事件

2024-10-01 17:30:57 发布

您现在位置:Python中文网/ 问答频道 /正文

有大量的例子展示了如何使用下面的selenium输入文本

driver.execute_script('cm.setValue("text")');

这是可行的,但不是我们的“硒”。我们想模拟实际的键盘按键操作,比如selenium中的send_keys函数。我们创建了一个enterFormData它获取一个元素并使用driver.send_密钥()(例如,带有ID的文本区域,我们可以很容易地模拟输入)。我们如何在CodeMirror编辑器中模拟实际的按键操作?我们还希望能够测试热键(例如Ctrl-Shift-M),然后使用driver.get_截图()


Tags: 函数text文本sendexecutedriverseleniumscript
2条回答

codemirror实例应该有一个隐藏的文本区域来捕获键盘事件。。。在

我做了这样的事:

driver.findElement(By.css('.CodeMirror textarea'))
    .sendKeys('text', 
        webdriver.Key.chord(webdriver.Key.CONTROL, webdriver.Key.SHIFT, "m"));

此位:webdriver.Key.chord(webdriver.Key.CONTROL, webdriver.Key.SHIFT, "m")模拟(Ctrl+Shift+M)

selenium要检测键盘事件,首先必须将codemirror置于焦点位置。在

你可以这样做:

/* getting codemirror element */
WebElement codeMirror = driver.findElement(By.className("CodeMirror"));

/* getting the first line of code inside codemirror and clicking it to bring it in focus */
WebElement codeLine = codeMirror.findElements(By.className("CodeMirror-line")).get(0);
codeLine.click();

/* sending keystokes to textarea once codemirror is in focus */
WebElement txtbx = codeMirror.findElement(By.cssSelector("textarea"));
txtbx.sendKeys("Hello World");

相关问题 更多 >

    热门问题