java - 无法单击 TreeView 中的元素 - Selenium webdriver java

标签 java selenium selenium-webdriver treeview

我试图通过点击 TreeView 来到达一个元素。我可以点击第一级“设备”,但无法进入下一级别。

这是浏览器窗口的屏幕截图。

Tree view of the webapp

这是 HTML 代码的简化版本。

<ul class="jstree-container-ul jstree-children" role="group">
  <li role="treeitem" id="devices" class="jstree-node jstree-open">
    <i class="jstree-icon jstree-ocl" role="presentation"></i>
    <a class="jstree-anchor" id="devices_anchor">
      <i class="jstree-icon jstree-themeicon" role="presentation"></i> Devices
    </a>
    <ul role="group" class="jstree-children" style="">
      <li role="treeitem" id="j1_3" class="jstree-node jstree-open">
        <i class="jstree-icon jstree-ocl" role="presentation"></i>
        <a class="jstree-anchor" id="j1_3_anchor">
          <i class="jstree-icon jstree-themeicon" role="presentation"></i> HANDSET
        </a>
        <ul role="group" class="jstree-children" style="">
          <li role="treeitem" id="j1_6" class="jstree-node jstree-open">
            <i class="jstree-icon jstree-ocl" role="presentation"></i>
            <a class="jstree-anchor  jstree-clicked" href="#" tabindex="-1" id="j1_6_anchor">
              <i class="jstree-icon jstree-themeicon" role="presentation"></i> Apple
            </a>
            <ul role="group" class="jstree-children" style="">
              <li role="treeitem" id="j1_15" class="jstree-node  jstree-leaf">
                <i class="jstree-icon jstree-ocl" role="presentation"></i>
                <a class="jstree-anchor" id="j1_15_anchor">
                  <i class="jstree-icon jstree-themeicon" role="presentation"></i> iPhone 5s
                </a>
              </li>
              <li role="treeitem" id="j1_16" class="jstree-node  jstree-leaf">
                <i class="jstree-icon jstree-ocl" role="presentation"></i>
                <a class="jstree-anchor" id="j1_16_anchor">
                  <i class="jstree-icon jstree-themeicon" role="presentation"></i> iPhone 6
                </a>
              </li>
            </ul>
          </li>
        </ul>
      </li>
    </ul>
</ul>

这是我如何能够单击“设备” TreeView 中的第一个 TreeMap 标元素

driver.findElement(By.cssSelector("#devices > i.jstree-icon.jstree-ocl")).click();

之后我不知道如何点击 HANDSET --> Apple --> iPhone 6 旁边的后续树形图标到达 iPhone 6 页面。

我只是不能使用 ID,因为一旦将另一个项目添加到 TreeView 中,它们就会发生变化。

我试过类似的方法,但对我不起作用。

driver.findElement(By.xpath("//*[contains(text(),'HANDSET')]/../preceding-sibling::i[@class='i.jstree-icon.jstree-ocl']")).click();

更新:对于那些来到这里的人,这是我使用@santhosh kumar 的回答解决它的方法:

driver.findElement(By.xpath("//a[contains(text(),'HANDSET')]/parent::li/i")).click();
driver.findElement(By.xpath("//a[contains(text(),'Apple')]/parent::li/i")).click();

最佳答案

你能试试这个xpath吗,

"//a[contains(text(),'HANDSET')]/parent::li/i"

希望这对您有所帮助。谢谢。

关于java - 无法单击 TreeView 中的元素 - Selenium webdriver java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45976264/

相关文章:

java - Oracle JDBC 刷新共享池不工作

javascript - 如何使用 wdio 处理弹出窗口?

android - 滑动方法在android自动化测试中不起作用

java - 针对特定操作的动态对象与单例

java - 泛型 Class<T> 中的 T 类是否可以从另一个类分配?

javascript - 从下拉菜单中逐页阅读 - 在第二页找不到下拉菜单

python - 使用 Selenium Python chrome webdriver 发推文

java - 通过 selenium 选择一个 javascript 下拉列表?

java - 当当前类已经扩展了另一个类时实现抽象类的方法

javascript - Selenium Python : Unable to get element by id/name/css selector