0

I am building a Windows 8.1 Phone app which needs to access elements from a web page. The elements I need to access are fairly deep inside the page so I need to find a simple way to access those elements without using XPATH (because XPATH is not supported on Windows Phone 8.1 to my understanding).

The method below is the method I am using to get the page (which works correctly) as far as loading the document from the web page is concerned. To give you an example HTML element I will need to access, consider the following example:

<html>
<head>..</head>
<body>
    <form>
        <div class="1"></div>
        <div class="2">
            <p>The text I wish to access</p>
        </div>
        <div class="3"></div>
    </form>
    <p>...</p>
</body>
</html>

And the method I need to access the text

private async Task GetHtmlDocument(string url)
{
    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
    request.Credentials = new LoginCredentials().Credentials;

    try
    {
        WebResponse myResponse = await request.GetResponseAsync();
        HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();
        htmlDoc.OptionFixNestedTags = true;
        htmlDoc.Load(myResponse.GetResponseStream());


        var body = htmlDoc.DocumentNode.Descendants("body").First();
        var form = body.Descendants("form").First();

    }
    catch (...){ ... }
}

EDIT: Forgot to mention that Descendants("form") is returning nothing useful (even though body.InnerHTML contains a form element).

The following code prints all of the HTML code which can be found by inspecting the Body element in the browser for "body", but for "form" InnerHtml is empty ("") and the element has no children.

HtmlNode body = htmlDoc.DocumentNode
            .Descendants("body")
            .FirstOrDefault();
HtmlNode form = htmlDoc.DocumentNode
            .Descendants("body")
            .FirstOrDefault()
            .Descendants("form")
            .FirstOrDefault();

Debug.WriteLine(body.InnerHtml);
Debug.WriteLine(form.InnerHtml);
dev-masih
  • 4,188
  • 3
  • 33
  • 55
Josh Brass
  • 147
  • 2
  • 15

1 Answers1

0

I think this will help you. Apparently you need to remove from the flagged element list.

Community
  • 1
  • 1
thang2410199
  • 1,932
  • 2
  • 17
  • 18