Search code examples
javascriptc#selenium-webdriverjstree

Expanding all Nodes of jstree using selenium-webdriver


I apologize for my bad english, but i really need help. I am trying to automate the Internet Explorer in c# using selenium-webdriver to fill a formular on an external website (btw. i have good intentions :D).

My Problem is to select nodes in a jstree which are loaded dynamically. After selecting a node a new ul-element is generated by the website and i can't find them with the webdriver of selenium via xPathes until the parent node is clicked and these where generated. Can you help me extanding all nodes in the jstree with javascript or codebehind? The hml code looks like this.

<div id="tree" class="jstree jstree-0 jstree-focused jstree-classic">
<ul>
<li class="jstree-opened">
<a class="" href="#">
<ins class="jstree-icon"></ins>
Nodetext
</a>
<ul></ul>
</li>
<li class="jstree-closed">
<a class="" href="#">
<ins class="jstree-icon"></ins>
Name of Node
</a>
</li>
</ul>

Picture how the above code is looking like.

enter image description here


Solution

  • I tried below code snippet and worked on jstree demo: https://www.jstree.com/demo/

    ('#jstree_demo').jstree('open_all');
    

    Please check above code can work on your jstree firstly. Then you can use selenium api: driver.execute_script() to execute above code on browser as below:

    Java example:

    JavascriptExecutor js = (JavascriptExecutor) driver;
    js.executeScript("return ('#jstree_demo').jstree('open_all');");
    

    JavaScript example:

    driver.executeScript("return ('#jstree_demo').jstree('open_all');");
    

    Python example:

    driver.execute_script("return ('#jstree_demo').jstree('open_all');");