Selenium测试的XPath标识

时间:2016-12-29 11:11:14

标签: javascript java html selenium xpath

我是Selenium和XPath的新手。能帮到我吗?我为以下结构正确识别了XPath:

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("button").click(function(){
   
       $('.cloneitem').not('.cloned').clone().addClass('cloned').appendTo('body');
    });
});
</script>
</head>
<body>

<p class="cloneitem">This is a paragraph.</p>

<button>Clone all p elements, and append them to the body element</button>

</body>
</html>

我已将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>

但是我遇到了("//div[@class='menutext']/descendant::ul[@class='children sub-menu']/a[@href='http://www.ufthelp.com/p/uft.html']")); 。请帮我一样。感谢

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

  1. 班级&#34; menutext&#34;缺少。
  2. 如果您的目标是锁定锚标记,那么我建议使用替代而不是xpath。请尝试
  3. `

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

    `

答案 2 :(得分:0)

XPATH:

// a [@title =&#39; UFT&#39;]

//一个[文本()=&#39; UFT&#39;]

CSS:

a [title =&#39; UFT&#39;]