1

My goal is to get a count of all links on a page and then loop through that count and get the text of all links on any given page. I'm also using C# (in visual studio express).

What is/would be the xpath to get the text of the visible links on the page? I'm pretty green with xpath and I'm not sure what I'm missing.

Here's the count variable:

decimal count = selenium.GetXpathCount("//a[@href]");

So that returns the number of links with the @href attribute on the page. So far so good. But, in looping through it is reporting back to me the text of links that are hidden on the page:

for (int i = 1; i <= count; i++)
{
    linkTxt = selenium.GetText("xpath=(//a[@href][" + i + "])/text()");
    if (selenium.IsPresent("xpath=(//a[@href])[" + i + "]") && selenium.IsVisible("xpath=(//a[@href])[" + i + "]"))
    {
        MessageBox.Show(linkTxt, "Link Text", MessageBoxButton.OK);
    }
}

That should work, but I think part of the problem is becuase while some of the //div 's that are parents of the //a links are NOT hidden, some of the //tables ARE hidden.

Example html:

<table border="0" cellpadding="0" cellspacing="0" id="tableIDTwo" class="navigation-inside product-rightonly" aria-hidden="false" style="">
<colgroup>
    <col>
    <col>
    <col>        
</colgroup>
<tbody>
    <tr>
        <td>
            <div class="background-orange" id="someID" aria-hidden="false" style="white-space: nowrap;">1</div>
        </td>
        <td align="left" class="header-link" aria-hidden="false" style="vertical-align: middle; white-space: nowrap;">
            <div class="gwt-HTML" id="VisibleLink" aria-hidden="false" style="white-space: nowrap;">
                <a href="javascript:;">Visible Link Text</a>
                <a></a>
            </div>
        </td>
    </tr>    
</tbody>    
</table>
<div>
    <table>
        <tbody>
            <tr>
                <td>
                    <table>
                        <tbody>
                            <tr>
                                <td>
                                    <div>
                                        <div>
                                            <table border="0" cellpadding="0" cellspacing="0" id="tableIDone" class="grid-sf-table-inside" aria-hidden="true" style="display: none;">
                                                <colgroup>
                                                    <col>
                                                    <col>
                                                </colgroup>
                                                <tbody>
                                                    <tr aria-hidden="false" style="">
                                                        <td>
                                                            <div class="gwt-HTML" id="link1(1,0)" aria-hidden="false" style="white-space: normal;">
                                                                <a href="javascript:;">Hidden Link Text</a>
                                                            </div>
                                                        </td>
                                                        <td>             
                                                </tbody>        
                                            </table>
                                        </div>
                                    </div>
                                </td>
                            </tr>
                        </body>
                    </table>
                </td>
            </tr>
        </tbody>
    </table>
</div>
4

0 回答 0