Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to type text into Code Mirror line using selenium?

I'm using IE to type in text into a text box that is labeled as a CodeMirror-line. I've tried javascript executor but that seems to only be for looks as when I try to save what I typed it disappears. How do I write text to the code mirror so it will stay? Below is the xpath I am using with the javascript executor to write to the Code mirror.

JavascriptExecutor js = (JavascriptExecutor) driver;

js.executeScript("arguments[0].innerHTML = 'Some text';", element);

@FindBy(xpath = "//*[@id='md-tab-content-1-0']/div/div/div[2]/jw-editor/div[1]/div/div[6]/div[1]/div/div/div/div[5]/pre")
    public WebElement element;

I've also seen something about using setValue to achieve this as well but haven't found a clear answer on how? Any help is appreciated. Thanks.

like image 333
Kresh Avatar asked Oct 24 '25 03:10

Kresh


1 Answers

For selenium to detect the keyboard events, you'll first have to bring codemirror into focus.

You can do something like this:

/* 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");
like image 195
Abhishek Hingorani Avatar answered Oct 25 '25 19:10

Abhishek Hingorani



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!