在 C# 中一一重定向到 HTML table 的特定列中出现的每个 link
Redirect to every link present in particular column of a HTML table one by one in C#
我从网页的数据 table 中检索了一个 table,或者简单地说我有一个 table,其中第一列包含不同的名称,所有这些名称都是 hyperlink.Now 我的要求ui 是我需要用 C# 或编码 ui 逐一单击每个名称。我该怎么做?
看起来像这样:
[TestMethod]
public async Task TestAllTheLinks()
{
BrowserWindow browserWindow = BrowserWindow.Launch("mywebsiteurl");
HtmlTable table = new HtmlTable(browserWindow);
table.SearchProperties.Add(HtmlTable.PropertyNames.Id, "tableId"); // or other search properties
List<Exception> failureLinks = new List<Exception>();
using (var client = new HttpClient())
{
for(int rowIndex = 0, max = table.RowCount; rowIndex < max; rowIndex++)
{
HtmlCell tableCell = table.GetCell(rowIndex, 0);
HtmlHyperlink link = new HtmlHyperlink(tableCell);
// are you sure you want to click?
// how are you going to test rest of links if you nav away?
Mouse.Click(link);
// or would you rather just send an http request to that url to see if it is successful
string href = link.Href;
var result = await client.GetAsync(href);
if (!result.IsSuccessStatusCode)
{
failureLinks.Add(new Exception($"Link failed: {href}"));
}
}
}
if(failureLinks.Any()){
throw new AggregateException(failureLinks);
}
}
我从网页的数据 table 中检索了一个 table,或者简单地说我有一个 table,其中第一列包含不同的名称,所有这些名称都是 hyperlink.Now 我的要求ui 是我需要用 C# 或编码 ui 逐一单击每个名称。我该怎么做?
看起来像这样:
[TestMethod]
public async Task TestAllTheLinks()
{
BrowserWindow browserWindow = BrowserWindow.Launch("mywebsiteurl");
HtmlTable table = new HtmlTable(browserWindow);
table.SearchProperties.Add(HtmlTable.PropertyNames.Id, "tableId"); // or other search properties
List<Exception> failureLinks = new List<Exception>();
using (var client = new HttpClient())
{
for(int rowIndex = 0, max = table.RowCount; rowIndex < max; rowIndex++)
{
HtmlCell tableCell = table.GetCell(rowIndex, 0);
HtmlHyperlink link = new HtmlHyperlink(tableCell);
// are you sure you want to click?
// how are you going to test rest of links if you nav away?
Mouse.Click(link);
// or would you rather just send an http request to that url to see if it is successful
string href = link.Href;
var result = await client.GetAsync(href);
if (!result.IsSuccessStatusCode)
{
failureLinks.Add(new Exception($"Link failed: {href}"));
}
}
}
if(failureLinks.Any()){
throw new AggregateException(failureLinks);
}
}