如何选择<a>标签 HtmlAgilityPack



这是我试图选择的屏幕截图,我从控制台复制了Xpath,但当我运行程序时,它声明它为null:Anchor标记。

我曾尝试将Xpath复制到标记及其上方的标记,它们都返回一个null值,但当我在浏览器中运行查询并检查页面时,它们都有一个值。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using HtmlAgilityPack;
namespace AccessID2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
#region UI Event Handlers
private void cmdGo_Click(object sender, EventArgs e)
{
RestClient rClient = new RestClient();
rClient.endPoint = txtUrl.Text;
debugOutput("Searching for Product");
string strResponse = string.Empty;
strResponse = rClient.makeRequest();
debugOutput(strResponse);
var html = strResponse;
var htmlDoc = new HtmlAgilityPack.HtmlDocument();
htmlDoc.LoadHtml(html);
var htmlNodes = htmlDoc.DocumentNode.SelectNodes("//*[@id='anch_114']");
txtID.Text = htmlNodes.ToString();
}
#endregion
private void debugOutput(string strDebugText)
{
try
{
System.Diagnostics.Debug.Write(strDebugText + Environment.NewLine);
txtResponse.Text = txtResponse.Text + strDebugText + Environment.NewLine;
txtResponse.SelectionStart = txtResponse.TextLength;
txtResponse.ScrollToCaret();
}
catch (Exception ex)
{
System.Diagnostics.Debug.Write(ex.Message, ToString() + Environment.NewLine);
}
}

private void label3_Click(object sender, EventArgs e)
{
}
}
}

只需选择锚点元素:

var htmlNodes = htmlDoc.DocumentNode.SelectNodes("//a[@id='anch_114']");

最新更新