<a> tag according to the URL

时间:2017-06-13 13:45:21

标签: xpath selenium-ide

I am using the selenium firefox plugin and run across an issue of trying to select the nth element of multiple tags with the same value of the href attribute but just having the id at the last change.

After looking at the screenshot the argument:

//a[contains(@href, '/setup/goal_add_edit/Orm_Institution/1/')]

must be the 2nd element I tried:

//a[contains(@href, '/setup/goal_add_edit/Orm_Institution/1/')][2]

and got an error in the log:

[error] Element //a[contains(@href, '/setup/goal_add_edit/Orm_Institution/1/')][2] not found Selenium IDE on firefox

的第n个元素

以下是HTML示例:

<table class="table table-bordered ">
<thead>
  <tr>
     <td class="col-md-10">Goal</td>
     <td class="col-md-2 text-center">Actions</td>
  </tr>
</thead>
<tbody>
  <tr>
     <td>
        FF_selenium_test                    
     </td>
     <td class="text-center">
        <a href="/setup/goal_add_edit/Orm_Institution/1/3" data-toggle="ajaxModal" class="btn btn-block " title="Edit">
        <span class="btn-label-icon left fa fa-pencil-square-o" aria-hidden="true"></span>
        Edit                            </a>
        <a href="/setup/goal_delete/Orm_Institution/1/3" data-toggle="deleteAction" message="Are you sure ?" class="btn btn-block " title="Delete">
        <span class="btn-label-icon left fa fa-trash-o" aria-hidden="true"></span>
        Delete                            </a>
     </td>
  </tr>
  <tr>
     <td>
        FF_selenium for editing                    
     </td>
     <td class="text-center">
        <a href="/setup/goal_add_edit/Orm_Institution/1/4" data-toggle="ajaxModal" class="btn btn-block " title="Edit">
        <span class="btn-label-icon left fa fa-pencil-square-o" aria-hidden="true"></span>
        Edit                            </a>
        <a href="/setup/goal_delete/Orm_Institution/1/4" data-toggle="deleteAction" message="Are you sure ?" class="btn btn-block " title="Delete">
        <span class="btn-label-icon left fa fa-trash-o" aria-hidden="true"></span>
        Delete                            </a>
     </td>
  </tr>
</tbody>
</table>

1 个答案:

答案 0 :(得分:1)

我不知道您的具体环境,但我认为您的代码应该有效...尝试在XPath

之前的[2]周围放置一个问题

一些可能的问题:

  • 区分大小写
  • 名称空间(在你的情况下不要这么认为)
  • 您测试的现有HTML没有第二个<a>具有此href
  • 由于之前的步骤,当前节点不是根节点。如果您当前的节点在某个地方完成了路径,那么使用//a搜索将不会发现更高的出现...

试试这样:

(//a[@href='/setup/goal_add_edit/Orm_Institution/1/'])[2]

这应该找到所有<a>元素,其中href - 属性具有给定值。然后它需要第二次出现...

使用SQL-Server进行测试,但在您的环境中应该类似:

DECLARE @ITEM XML=
N'<html>
  <body>
  <a href="YourURL">Test1</a>
  <SomeOther><inner><a href="YourURL">Testinner</a></inner></SomeOther>
  <a href="YourURL">Test2</a>
  <a href="YourURL">Test3</a>
  </body>   
  </html>';

- 适用于所有人

SELECT @ITEM.value('(//*[local-name()="a" and @href="YourURL"]/text())[2]','nvarchar(max)');

- 更好,只查找<a>

SELECT @ITEM.value('(//a[@href="YourURL"]/text())[2]','nvarchar(max)');

- 返回NULL第二 <a> <SomeOther> - 但是[1]它会返回与上面相同的内容......

SELECT @ITEM.query('/html/body/SomeOther').value('(//a[@href="YourURL"])[2]','nvarchar(max)');

更新:使用您的给定样本

用你的样品再试一次。在这里工作(SQL Server),也应该与你一起工作......

DECLARE @ITEM XML=
N'<table class="table table-bordered ">
<thead>
  <tr>
     <td class="col-md-10">Goal</td>
     <td class="col-md-2 text-center">Actions</td>
  </tr>
</thead>
<tbody>
  <tr>
     <td>
        FF_selenium_test                    
     </td>
     <td class="text-center">
        <a href="/setup/goal_add_edit/Orm_Institution/1/3" data-toggle="ajaxModal" class="btn btn-block " title="Edit">
        <span class="btn-label-icon left fa fa-pencil-square-o" aria-hidden="true"></span>
        Edit                            </a>
        <a href="/setup/goal_delete/Orm_Institution/1/3" data-toggle="deleteAction" message="Are you sure ?" class="btn btn-block " title="Delete">
        <span class="btn-label-icon left fa fa-trash-o" aria-hidden="true"></span>
        Delete                            </a>
     </td>
  </tr>
  <tr>
     <td>
        FF_selenium for editing                    
     </td>
     <td class="text-center">
        <a href="/setup/goal_add_edit/Orm_Institution/1/4" data-toggle="ajaxModal" class="btn btn-block " title="Edit">
        <span class="btn-label-icon left fa fa-pencil-square-o" aria-hidden="true"></span>
        Edit                            </a>
        <a href="/setup/goal_delete/Orm_Institution/1/4" data-toggle="deleteAction" message="Are you sure ?" class="btn btn-block " title="Delete">
        <span class="btn-label-icon left fa fa-trash-o" aria-hidden="true"></span>
        Delete                            </a>
     </td>
  </tr>
</tbody>
</table>';

- 两次尝试都返回“编辑”

SELECT @ITEM.value('(//*[local-name()="a" and contains(@href, "/setup/goal_add_edit/Orm_Institution/1/")]/text())[2]','nvarchar(max)');

SELECT @ITEM.value('(//a[contains(@href, "/setup/goal_add_edit/Orm_Institution/1/")]/text())[2]','nvarchar(max)');