Take the 2-minute tour ×
Software Quality Assurance & Testing Stack Exchange is a question and answer site for software quality control experts, automation engineers, and software testers. It's 100% free, no registration required.

The following snapshot is what I see when I do a view of class and Ids in using IE Developer tool. It consists of a table and inside the table there are links. I need to read all the link names, and store the link names in a string array.

http://stackoverflow.com/questions/6510758/c-selenium-read-rows-from-html-table-until-the-last-row-is-found-and-store-in

When I use Firebug to get the Xpath of each of the links, I get:

/html/body/table/tbody/tr/td[2]/table/tbody/tr/td[2]/font/a

/html/body/table/tbody/tr/td[2]/table/tbody/tr/td[4]/font/a

/html/body/table/tbody/tr/td[2]/table/tbody/tr[3]/td[2]/font/a

/html/body/table/tbody/tr/td[2]/table/tbody/tr[3]/td[4]/font/a

/html/body/table/tbody/tr/td[2]/table/tbody/tr[5]/td[2]/font/a

/html/body/table/tbody/tr/td[2]/table/tbody/tr[5]/td[4]/font/a

I had the following method initially, but QuickWatch showed arrTopics as null. It should have given me at least 10 labels. The value of arrTopics should have been something like {"Administration", "Banking", "Marketing", ...} I think this has to do with my xpath (("//a[@class='wlcategoryLinkBold']/@href").

 public List<string> Search(ISelenium Sel)    
{        
    Sel.Click(//*[@id="Search_Tab_Search"]);       
    List<string> topics = new List<string>();        
    int se = (int) Sel.GetXpathCount("//a[@class='wlcategoryLinkBold']/@href");
    for (int i = 1; i <= se; i++)
        {
            if (Sel.IsElementPresent("//a[@class='wlcategoryLinkBold']/@href[" + i + "]"))
            {
                string value = Sel.GetSelectedLabel("//a[@class='wlcategoryLinkBold']/@href[" + i + "]");
                topics.Add(value);
            }
        }

        string[] arrTopics = topics.ToArray();
        return arrTopics;    
}

How do I use

/html/body/table/tbody/tr/td[2]/table/tbody/tr/td[2]/font/a

/html/body/table/tbody/tr/td[2]/table/tbody/tr/td[4]/font/a

/html/body/table/tbody/tr/td[2]/table/tbody/tr[3]/td[2]/font/a

/html/body/table/tbody/tr/td[2]/table/tbody/tr[3]/td[4]/font/a

/html/body/table/tbody/tr/td[2]/table/tbody/tr[5]/td[2]/font/a

/html/body/table/tbody/tr/td[2]/table/tbody/tr[5]/td[4]/font/a
.
.
.

instead of ("//a[@class='wlcategoryLinkBold']/@href) in the Sel.GetXpath()? I see that I need to have another loop that takes care of tr[3], tr[5], td[2], td[4].

share|improve this question
1  
What is the question? Why does QuickWatch say this? –  Ethel Evans Jun 28 '11 at 23:50
    
Your code fragment and your QuickWatch error message do not match: the code fragment uses "//a[@class=" whereas the QuickWatch message uses "//*[class=". Is it possible the QuickWatch fragment refers to some other place in your code? –  user246 Jun 29 '11 at 12:25
    
Please rephrase the title as a question. –  user246 Jun 29 '11 at 12:30
add comment

1 Answer 1

up vote 2 down vote accepted

Here are some potential problems:

  • //a[@class='wlcategoryLinkBold']/@href is an Xpath to an href, not to an "a" tag. Furthermore, //a[@class='wlcategoryLinkBold']/@href[i] is the Xpath to the i'th href of an "a" tag, which is nonsensical. I believe you want to use (//a[@class='wlcategoryLinkBold'][@href])[i].
  • GetSelectedLabel gets option label (visible text) for selected option in the specified select element. You seem to want to return the label for an "a" tag, so you want to use GetText.

Please try this instead:

public List<string> Search(ISelenium Sel)    
{        
    Sel.Click(//*[@id="Search_Tab_Search"]);       
    List<string> topics = new List<string>();        
    int se = (int) Sel.GetXpathCount("//a[@class='wlcategoryLinkBold'][@href]");
    for (int i = 1; i <= se; i++)
        {
           string value = Sel.GetText("(//a[@class='wlcategoryLinkBold'][@href])[" + i + "]");
           topics.Add(value);
        }

    string[] arrTopics = topics.ToArray();
    return arrTopics;    
}
share|improve this answer
    
@user246: Thanks for your response. I tried what you suggested, but it is still not working. This time, I added a quick watch for int se = (int) Sel.GetXpathCount("//a[@class='wlcategoryLinkBold'][@href]"), and the value it shows me is 0. :( –  Maya Jun 29 '11 at 15:07
    
@Maya Do you have a tool you can use to experiment with Xpaths in a browser? I use Firefox with the FirePath and FireBug add-ons, but there are other options too. –  user246 Jun 29 '11 at 15:28
    
@user246: unfortunately, I cannot use the awesome firebug because this webpage is not compatible with firefox. Do you know any tool in IE to get xpaths? –  Maya Jun 29 '11 at 15:32
    
@Maya In FirePath, if you type "//a[@class='wlcategoryLinkBold'][@href]", what do you see? –  user246 Jun 29 '11 at 16:31
    
@user246: You mean in FireBUG search? I was actually able to view source the webpage, and save it, and open with firefox. Then use firebug to get xpath of the links in the rows and columns. Which gave me xpaths like: /html/body/table/tbody/tr/td[2]/table/tbody/tr/td[4]/font/a –  Maya Jun 29 '11 at 16:39
show 3 more comments

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.