有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Selenium测试的javascript XPath标识

我不熟悉Selenium和XPath。您能帮我正确识别以下结构的XPath吗:

<body>
<div id="body-wrapper">
<div id="outer-wrapper">
<div id="wrap2">
<div class="span-24">
<div style="clear: both;"/>
<div id="header-wrapper">
<div style="clear: both;"/>
<div class="span-24">
<div class="menu-secondary-container">
<ul class="menus menu-secondary">
<li>
<li>
<li class="">
<a class="sf-with-ul" href="#">
Automation
<span class="sf-sub-indicator">»</span>
<span class="sf-sub-indicator">»</span>
</a>
<ul class="children sub-menu" style="display: none; visibility: hidden;">
<li>
<a href="http://www.ufthelp.com/p/uft.html" title="UFT">UFT</a>
</li>

我已将XPath标识为:

("//div[@class='menutext']/descendant::ul[@class='children sub-menu']/a[@href='http://www.ufthelp.com/p/uft.html']"));

但我遇到了NoSuchElementException。请帮我做同样的事。谢谢


共 (3) 个答案

  1. # 1 楼答案

    1. 缺少“menutext”类
    2. 如果您的目标是锁定锚标记,那么我建议使用另一种方法,而不是xpath。请试一试

    `

    driver.findElement(By.tagName("a")) then(function(anchorTags) {
      anchorTags.getText().then(function(anchorText) {
        if (anchorText == UFT) {
          // do your thing here
        }
      })
    })
    

    `

  2. # 2 楼答案

    XPATH:

    //a[@title='UFT']

    或者

    //a[text()='UFT']

    CSS:

    a[title='UFT']

  3. # 3 楼答案

    你可以在firofox浏览器中使用firebug扩展轻松获取Xpath。只需按元素u进行选择即可获得xpath值。FirePath是一个Firebug扩展,它添加了一个用于编辑、检查和生成XPath的开发工具