Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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>
share|improve this question
1  
Unlike forum sites, we don't use "Thanks", or "Any help appreciated", or signatures on Stack Overflow. See "Should 'Hi', 'thanks,' taglines, and salutations be removed from posts?" –  codechurn Aug 13 '13 at 16:37
1  
noted, but I was being polite. –  Nacho Aug 13 '13 at 16:42
    
and I was trying to be helpful. We have all made the same mistakes. :) –  codechurn Aug 13 '13 at 16:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.