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.
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].
-
1What is the question? Why does QuickWatch say this?Ethel Evans– Ethel Evans2011年06月28日 23:50:59 +00:00Commented Jun 28, 2011 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– user2462011年06月29日 12:25:02 +00:00Commented Jun 29, 2011 at 12:25
-
Please rephrase the title as a question.user246– user2462011年06月29日 12:30:23 +00:00Commented Jun 29, 2011 at 12:30
2 Answers 2
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;
}
-
@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– Maya2011年06月29日 15:07:33 +00:00Commented Jun 29, 2011 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– user2462011年06月29日 15:28:34 +00:00Commented Jun 29, 2011 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– Maya2011年06月29日 15:32:10 +00:00Commented Jun 29, 2011 at 15:32
-
@Maya In FirePath, if you type "//a[@class='wlcategoryLinkBold'][@href]", what do you see?user246– user2462011年06月29日 16:31:15 +00:00Commented Jun 29, 2011 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/aMaya– Maya2011年06月29日 16:39:07 +00:00Commented Jun 29, 2011 at 16:39
You seem to be trying to select the a tags based on an attribute which is on a font tag. Instead, try the xpath selector //a[@target='SearchFrame']
to select all the links. You can use Driver.SelectElements to return these as an IEnumerable<IWebElement>
. Sorry if the selector isn't quite right, I'm trying this on my phone so can't check back to the DOM you pasted.
If you need the hrefs themselves, just project these using a lambda expression, such as:
query.Select(e => e.GetAttribute<string>("href"));
This will give you an IEnumerable<string>
containing the individual hrefs. Otherwise if you want the link texts, just select
query.Select(e => e.Text);