Here is a question input field. When clicked on, the h3 element with the name will disappear and the input tag will appear for entry of a new name.
<td style="width:92%;">
<h3 id="question_text_1846" onclick="return onClickQuestion(1846,'text');">test name</h3>
<input type="text" id="question_text_input_1846" onkeypress="return OnKeyPress(event, 1846,'text');" name="question_text_input_1846" onblur="return onBlurQuestion(1846,'text');" placeholder="Question Text" value="test" class="form-control myInput" style="display:none;" />
<script type="text/javascript">
$("#question_text_1846").html(unescape($("#question_text_1846").html()));
</script>
</td>
I am trying to set a new name by clearing the field and using sendkeys through selenium and possibly some JavaScript manipulations. However, the methods I have tried so far have not been successful.
element = driver.findElement(By.xpath("//h3[@id='question_text_"+ExtractQuestionTextInputID()+"']"));
actions = new Actions(driver);
actions.moveToElement(element);
actions.perform();
element.click();
element = driver.findElement(By.xpath("//input[@id='question_text_input_"+ExtractQuestionTextInputID()+"']"));
element.clear();
element.sendKeys("test_question_one");
I have also attempted it with JavaScript
String qtid = "question_text_" + ExtractQuestionTextInputID();
String qtiid = "question_text_input_" + ExtractQuestionTextInputID();
js.executeScript("document.getElementById("+qtid+").setAttribute('style', 'display: none;')");
js.executeScript("document.getElementById("+qtiid+").setAttribute('style', '')");
This method extracts the id from the tag attribute
public String ExtractQuestionTextInputID(){
String question_text_input_id = driver.findElement(By.xpath("//input[@value='New Question']")).getAttribute("id");
Pattern p = Pattern.compile("\\d+");
Matcher m = p.matcher(question_text_input_id);
String mid = new String();
while(m.find()) {
//System.out.println(m.group());
mid = m.group();
}
return mid;
}