Search code examples
javahtmlseleniumweb-testing

Selenium Java. Put text in a div element: element not interactable


I use Selenium, not for web testing. I use it to get & change the text of a Website. In an extra editor for change the content(in Confluence).

The Text contains different formatting errors and I want to remove/replace them. (In my Website there is a editor for the text and a button where you can wirte HTML code). I get the text by going to the dev element + using the “innerText”. After that I want to change the text, but it isn’t in an inputbox, the text contains only one div with some pre-elements which have span-elements

My problem: there is no HTML element like an inputbox but you can normal write in there. The Code saves the text your tipping in a div and in there are pre elements. Confluence took the text line for line and put in span Elements and splits it into different parts(i don't know the exact logic but I think it splits if there is a new snippet or space and so on). Because of that i can't buil it the same and it also would take too long time. I will do it with sendKeys(another way is also possible but i don't know it.)

HTML Editor: [1]: https://i.sstatic.net/6cpo9.png

Java Code: [1]: https://i.sstatic.net/xl6A4.png

I will do it with: //copy the text and put in in the editor by deleting the text with CONTROLE + A and put it in with CONTROLE + V

Clipboard systemClip = Toolkit.getDefaultToolkit().getSystemClipboard(); 

systemClip.setContents(new StringSelection(txt), null); 

text.sendKeys(Keys.CONTROL + "A");

 text.sendKeys(Keys.DELETE); 

text.sendKeys(Keys.CONTROL + "V");

but then I get an error:

Exception in thread "AWT-EventQueue-0" org.openqa.selenium.ElementNotInteractableException: element not interactable

I don' know why the editor is in scope and nothing is over it. Furthermore, it is possible when I do it "normal".

Thank you very much for your help!!! Tell me how i can ask better!

 //get Text
List<WebElement> getTextWait = (new WebDriverWait(driver, 10)).until(ExpectedConditions.numberOfElementsToBe(By.className("CodeMirror-lines"),1));
List<WebElement> getText = driver.findElements(By.className("CodeMirror-lines"));
WebElement text = getText.get(0);
text.getLocation();
txt = text.getText();
String textString = txt;
System.out.println(txt);

//Funktionsaufruf damit der text geändert wird
Main main = new Main();
if(main.AbsätzeCheckobx.isSelected()) {
  //Absätze werden entfernt
  //Absätze werden gesucht und durch nichts ersetzt
  //Wenn ein doppelter Absatz vorhanden ist wird er hier definiert(das sind alles absätze)
  txt = absätzeEntfernen(txt);
}
if (main.KlammernCheckbox.isSelected()) {
  //Wenn im Word „[…]“ gibst ist in confluence „\[…\]“    PROBLEM: In Wiki markups wird aus „\[…\]“ das „[…]“ und wenn ich dan die backslashes entferne dann fehlen im wiki markup die "[..]" -> Lösung: wiki markup entfernen
  txt = backslashKlammerEntfernen(txt);
}
if (main.DurchgestrichenerTextCheckbox.isSelected()) {
  //Durchgestrichener text: wieder normal ( nicht durchgestrichen)
  txt = duchgestrichenerTextEntfernen(txt);
}
if (main.SternCheckbox.isSelected()) {
  //(*) soll nicht stern werden/ sein
  txt = sternEntfernen(txt);
}
if (main.PfeilCheckbox.isSelected()) {
  // soll zu ->
  txt = pfeilVerbessern(txt);
}
if (main.BackslashCheckbox.isSelected()) {
  //Entfernen von \\
  txt = backslahsEntfernen(txt);
}
if (main.wikiMarkupCheckbox.isSelected()) {
  //Entfernt den Anfang des Markup und die ID muss noch durch ein
  txt = wikiMarkupEntfernen(txt);
}
if (main.wikiMarkupTableCheckbox.isSelected()) {
  //Entfernt wikiMarkups in einer tabelle
  txt = wikiMarkupTableEntfernen(txt);
}
textString = txt;
System.out.println(txt);

//((JavascriptExecutor) driver).executeScript("t1 = document.getElementsByClassName(\"CodeMirror-lines\"); t2 = t1[0];t3 = t2.getElementsByTagName(\"div\");t4 = t3[0]; t4.innerText = '';");
//((JavascriptExecutor) driver).executeScript("t6 = document.getElementById(\"source-editor-wrap-lines\");document.getElementById(\"source-editor-wrap-lines\").checked = true;");

//aktueller error... drückt anscheinend nicht den apply button oder apply wird verhindert oder text wird nicht angenommen und ist vielleicht nocht in datenbank o.ä. gespeichert


Clipboard systemClip = Toolkit.getDefaultToolkit().getSystemClipboard();
systemClip.setContents(new StringSelection(txt), null);

//Click on the first span Element to set focus
List<WebElement> SpanElementList = driver.findElements(By.className("CodeMirror-lines"));
WebElement SpanElement = SpanElementList.get(0);
List<WebElement> divElementList = SpanElement.findElements(By.tagName("div"));
WebElement divElement = divElementList.get(0);
List<WebElement> div2ElementList = divElement.findElements(By.tagName("div"));
WebElement div2Element = div2ElementList.get(2);
List<WebElement> preElementList = div2Element.findElements(By.tagName("pre"));
WebElement preElement = preElementList.get(0);
List<WebElement> span2ElementList = preElement.findElements(By.tagName("span"));
WebElement span2Element =span2ElementList.get(0);
span2Element.click();


text.sendKeys(Keys.CONTROL + "A");
text.sendKeys(Keys.DELETE);
text.sendKeys(Keys.CONTROL + "V");

HTML Code:

<div style="">
  <pre>
    <span class="cm-tag">&lt;p&gt;</span>Hallo<span class="cm-tag">&lt;/p&gt;</span>
  </pre>
  <pre>
    <span class="cm-tag">&lt;h1&gt;</span>Test<span class="cm-tag">&lt;/h1&gt;</span>
  </pre>
  <pre>
    <span class="cm-tag">&lt;h5</span>
    <span class="cm-attribute">id</span>
    =<span class="cm-string">"id"</span> 
    <span class="cm-attribute">class</span>
    =<span class="cm-string">"class"</span>
    <span class="cm-tag">&gt;</span> 
       This is the editor an how it works<span class="cm-tag">&lt;/h5&gt;</span>
  </pre>
  <pre> 
  </pre>
  <pre> 
  </pre>
  <pre>
  </pre>
</div>

Solution

  • I didn't find a "good" way so i made an robot who press CTRL + V and it worked.