1

How can I create a variable that has the id from one of my records on the index page?

I can use xpath to get at the anchor I want with:

storeText
//table[@id='districts']//tr/td/a[text()='Fairview Union']
id_of_another_district

but that gets the link name. I want to id for that record, i.e. in the original source code I have:

<tr>
<td>
<a href="/districts/31">Fairview Union</a>
<span class='small'><a href="/users/change_district/31">(Select)</a></span>
</td>
<td></td>
<td></td>
<td></td>
<td class='rightalign'>01/09/2012</td>
<td></td>
<td><a href="/districts/31/overview" class="report_link right"></a></td>
<td><a href="/districts/31" class="deleteicon" data-confirm="Are you sure you want to delete all data and schedules for Fairview Union?" data-method="delete" rel="nofollow" title="Delete Fairview Union"><img alt="Delete" src="/assets/icons/delete-a77f3c3c125b0817085648f284c98938.png" /></a></td>
</tr>

and what I want to get and store in a variable in selenium is the record number 31

4

2 回答 2

3

使用“存储”命令,您可以在“目标”列中执行 javascript 代码...

store | javascript{...} | myVar

在哪里...,将其替换为您需要返回 id 的 js。

[来自迈克尔的编辑]我最终使用了:

storeAttribute | //*[@class='small']/a[text()='(Select)']/@href        | hrefAttr
store          | javascript{storedVars['hrefAttr'].replace(/.*\//,'')} | myVar
echo           | ${myVar}

myVar 现在有了 ID,我可以执行以下操作:

open
/users/change_district/${id_of_another_district}
于 2013-06-05T18:15:48.430 回答
0

The answer of sircapsalot (edited by Michael Durrant) is ok, but it's a bit different in my environment, regarding how the javascript is executed: not with javascript{...} So, in my case it looks like this and it works fine:

<tr>
    <td>storeAttribute</td>
    <td>//html/body/table/tbody/tr[3]/a@href</td>
    <td>hrefAttr</td>
</tr>
<tr>
    <td>storeEval</td>
    <td>storedVars['hrefAttr'].replace(/.*=/,'')</td>
    <td>transportIdVar</td>
</tr>
<tr>
    <td>echo</td>
    <td>${transportIdVar}</td>
    <td></td>
</tr>

The href in my case looks like this:

http://example/local/IntegrationPick?partyId=8080595
于 2014-11-11T09:21:58.623 回答