C# - Html Agility Pack - Get option tag value that matches text content

bots c# html-agility-pack

Question

Using HTML Agility Pack, I'm creating an automated application for a website. I'm attempting to extract the size-specific HTML code from a product as each item on the website has a unique code.

The following is a sample product and the website: Click this

My current code is as follows:

var webGet = new HtmlWeb();
var doc = webGet.Load(txtLink.Text);

HtmlNodeCollection collection = doc.DocumentNode.SelectNodes("//*[contains(@id,'size')]");
foreach (var node in collection)
{
var Size = node.InnerHtml.ToString();
txtLog.Text += Environment.NewLine + Size;
}

The answer for the sample product is as follows:

<option value="33455">Small
<option value="33456">Medium
<option value="33457">Large
<option value="33458">XLarge

The html code for an item looks like this if you don't want to visit the website:

<select name="size" id="size"><option value="33455">Small</option>
<option value="33456">Medium</option>
<option value="33457">Large</option>
<option value="33458">XLarge</option>
</select>

What I want to occur is:

I choose a size and provide the product URL for the item. If the chosen size is discovered, just that size's size code will be returned. For instance, the program should return 33456 if I use the sample item and choose Medium.

1
1
3/30/2017 10:08:48 PM

Accepted Answer

Case-insensitive matching and LINQ are two ways to do this:

var size = "xlarge";
var option = document
    .GetElementbyId("size")
    .ChildNodes
    .Where(x => x.Name == "option"
        && x.NextSibling.InnerText
            .IndexOf(size, StringComparison.OrdinalIgnoreCase) != -1)
    .FirstOrDefault();

var result = option != null 
    ? option.Attributes["value"].Value
    : "not found";
Console.WriteLine("{0}: {1}", size, result);
0
3/30/2017 6:27:27 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow