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

bots c# html-agility-pack

Question

I am making an automation application using Html Agility Pack for a website. Each product on the website has a different html code for size and I'm trying to get that code from a product.

This is the website and an example product: Click here

Here is my current code:

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;
}

Here is the response for the example product:

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

If you don't want to check the website this is what the html code looks like for an item:

<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 happen:

I enter the product link for an item and select a size. If the size selected is found then return the size code for that size only. For example I use the example item and select Medium so the application should return 33456.

Accepted Answer

One way to achieve this using LINQ and case-insensitive matching:

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);



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why