FSharp.Data icon indicating copy to clipboard operation
FSharp.Data copied to clipboard

HtmlDocument's CssSelect fails when no html available

Open jonathantwite opened this issue 6 years ago • 2 comments

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

jonathantwite avatar Sep 24 '18 10:09 jonathantwite

Can you send a PR with the fix and a unit test?

ovatsus avatar Oct 14 '18 11:10 ovatsus

I ran into this problem today with FSharp.Data 3.3.3, and updating to the latest 4.0.0 fixed it.

Thank you :)

(This issue can probably be closed)

mlaily avatar Apr 07 '21 18:04 mlaily