jamietre / CsQuery

CsQuery is a complete CSS selector engine, HTML parser, and jQuery port for C# and .NET 4.
Other
1.16k stars 249 forks source link

Wrong Selection? #197

Open jeromekwek opened 9 years ago

jeromekwek commented 9 years ago

Hi,

I'm trying to use CsQuery to go through some pages of Thesaurus.com

Using the address http://www.thesaurus.com/browse/test as an example, the code I'm using is as follows:

CQ document = CQ.CreateFromUrl("http://www.thesaurus.com/browse/test");
                var selectionHtml = document.Select("#filters-0 > div.relevancy-block > div > ul:nth-child(1) > li:nth-child(1) > a");

Result from CsQuery:

{<a class="common-word" href="http://www.thesaurus.com/browse/search" data-id="1" data-category='{"name": "relevant-3", "color": "#fcbb45"}' data-complexity="1" data-length="1">...</a>}

Result from jQuery:

<a href="http://www.thesaurus.com/browse/analysis" class="common-word" data-id="5" data-category="{&quot;name&quot;: &quot;relevant-3&quot;, &quot;color&quot;: &quot;#fcbb45&quot;}" data-complexity="1" data-length="2" style="font-weight: 400; color: rgb(51, 51, 51); background-color: rgb(252, 187, 69);">
<span class="text">analysis</span>
<span class="star inactive">star</span>
</a>

Both are using the same dom selection (i.e. #filters-0 > div.relevancy-block > div > ul:nth-child(1) > li:nth-child(1) > a), but CsQuery is returning me "search" while jQuery is returning me "analysis", which is correct. Any idea what's going on?

BenJury commented 9 years ago

The HTML isn’t the same.

In your browser there is some JS running post fetch which changes the order, this won’t happen in CQ.

If you slow your browser down you can see it happen. (Grab attached.)

jeromekwek commented 9 years ago

Thanks for your quick reply, Ben!

In that case, is there any way for CsQuery to "wait" for the page to process completely (with the new order), and then return with the actual result as displayed?

Or are there any other workaround/s for this issue?

BenJury commented 9 years ago

It doesn’t run javascript, so no.