Search code examples
jquerypythonlistseleniumjstree

Select element in the same list as element with "title"


I have the following code:

<div id="filebrowser" class="jstree jstree-1 jstree-grid-cell jstree-default jstree-checkbox-selection" role="tree" aria-multiselectable="true" tabindex="0" aria-activedescendant="-3" aria-busy="false" aria-selected="false">
    <ul class="jstree-container-ul jstree-children" role="group">
        <li role="treeitem" aria-selected="false" aria-level="1" aria-labelledby="-3_anchor" aria-expanded="false" id="-3" class="jstree-node  jstree-last jstree-closed" aria-busy="false">
            <i class="jstree-icon jstree-ocl" role="presentation"></i>
            <a class="jstree-anchor jstree-grid-col-0" href="#" tabindex="-1" id="-3_anchor" title="/">
                <i class="jstree-icon jstree-checkbox" role="presentation"></i>
                <i class="jstree-icon jstree-themeicon glyphicon glyphicon-folder-close jstree-themeicon-custom" role="presentation"></i>/
            </a>
        </li>
    </ul>
</div>

Screenshot

For my Selenium testing I need to click on the <i class="jstree-icon jstree-ocl">, which is marked red in the screenshot. I would like to click on this in the list that also has and <a> element that has a title= attribute. Is that possible?

For the background: I would like to select different files in given jstree and point on these files by setting their names as global values. If I would like to select "/", "etc/", "testdirectory" and then "test_file.txt".


Solution

  • To click on the WebElement represented as <i class="jstree-icon jstree-ocl"> you can use the following line of code :

    driver.find_element_by_xpath("//li[@role='treeitem']/i[@class='jstree-icon jstree-ocl' and @role='presentation']").click()
    

    But looking at the HTML seems we may have to invoke click() method on the <a> tag as follows :

    driver.find_element_by_xpath("//li[@role='treeitem']//a[@class='jstree-anchor jstree-grid-col-0']").click()