Skip to content

HtmlDocument's CssSelect fails when no html available #1212

@jonathantwite

Description

@jonathantwite

Apparently the <html> tag is optional on a webpage and so doesn't always exist. When it doesn't exist, HtmlDocument's CssSelect (and possible therefore, other functions) fail citing "No element html found".

For example, this works

#r @"..\packages\FSharp.Data.2.4.6\lib\net45\FSharp.Data.dll"
open FSharp.Data
open System.IO
open System.Text
HtmlDocument.Load(new MemoryStream(Encoding.UTF8.GetBytes(@"<html><body><h1 class='title'>Hi</h1></body></html>"))) 
|> (fun html -> html.CssSelect(".title")) 
|> printfn "%A"

but this doesn't

#r @"..\packages\FSharp.Data.2.4.6\lib\net45\FSharp.Data.dll"
open FSharp.Data
open System.IO
open System.Text
HtmlDocument.Load(new MemoryStream(Encoding.UTF8.GetBytes(@"<body><h1 class='title'>Hi</h1></body>"))) 
|> (fun html -> html.CssSelect(".title")) 
|> printfn "%A"

giving

System.Exception: No element html found!
   at FSharp.Data.HtmlDocumentExtensions.Html(HtmlDocument doc) in C:\Git\FSharp.Data\src\Html\HtmlOperations.fs:line 858
   at FSharp.Data.CssSelectorExtensions.CssSelectorExtensions.CssSelect(HtmlDocument doc, String selector) in C:\Git\FSharp.Data\src\Html\HtmlCssSelectors.fs:line 385
   at <StartupCode$FSI_0006>.$FSI_0006.main@()
Stopped due to error

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions