Selenium webdriver for handling dynamic ckeditors

241 views Asked by At

I am currently doing scraping using selenium in java for a website. Most of the pages I scrap contains ckeditor. I am currently using javascript to get the data from ckeditor using javascriptexecutor executescript method

CKEDITOR.instances['locator name/id/class'].getData()

but the problem here is the locator is changing dynamically I also found a way to get the name of the locator by getAttribute(name) method and used it in javascript but the problem here is some ckeditors use id , some use names or classes and mostly all the attributes(id,class,name) are used in html

Sample html

<textarea id="cm_ckeditor name="profile_description" style="width: 100%; height: 200px; visibility: hidden; display: none;"></textarea>

here if i use CKEDITOR.instances['profile_description'].getData() it is failing

It returns the data correctly when I use CKEDITOR.instances['cm_ckeditor'].getData() similarly there are scenarios in which the name works and id fails

1

There are 1 answers

0
Vicky On BEST ANSWER

From your question i guess you want to get data from ckeditor whose locator is changing dynamically

    var myinstances = [];

    for(var i in CKEDITOR.instances) {

    CKEDITOR.instances[i]; 

    CKEDITOR.instances[i].name;

    CKEDITOR.instances[i].value;  

    CKEDITOR.instances[i].updateElement();

    myinstances[CKEDITOR.instances[i].name] = CKEDITOR.instances[i].getData(); 

    return myinstances[CKEDITOR.instances[i].name];

}

You can use this javascript inside javascript executor

 JavaScriptExecutor js=(JavaScriptExecutor) driver;

 String ckeditor_data=(String) js.executeScript("function getdata(){var myinstances = []; for(var i in CKEDITOR.instances) { CKEDITOR.instances[i]; CKEDITOR.instances[i].name; CKEDITOR.instances[i].value;CKEDITOR.instances[i].updateElement(); myinstances[CKEDITOR.instances[i].name] = CKEDITOR.instances[i].getData(); return myinstances[CKEDITOR.instances[i].name];}}if ( CKEDITOR.status == 'loaded' ) {getdata();}");

 System.out.println(ckeditor_data);

Hope this helps you...kindly get back if you have any queries