Search code examples
c#htmlwinformsweb-scrapingwatin

How can I find and scrape by Class using WatiN?


I am using WatiN and trying to scrape an image URL from a weblink, based on the fields class. Viewing the sites code the images info displays as this:

//images code
<div class="doc-banner-icon">
    <img src="https://website.com/image.jpg">
</div>

//text code
<div id="doc-original-text">
    Once upon a time, in a land far far away...
</div>

What I want to do is use a WatiN call to find that img link. I thought I could use something like the Find.ByClass() call to find specifically that area of the code, but I can't seem to figure out how to get the line of text contained within that class. When I use the Find.ById() on a different field and sent to string it pulls the text content of that area. Below is what I am trying.

using (myIE)
{
    //loads the website
    myIE.GoTo(txtbxWeblink.Text);            

    string infoText = myIE.Div(Find.ByClass("doc-banner-icon")).ToString();

    //This will successfully return the text fields text.
    string imageText = myIE.Div(Find.ById("doc-original-text")).ToString();
}

EDIT - It appears that I may need to use a different call on myIE, there is also myIE.Image, myIE.Link etc, I don't know much about this all still so not sure if Div is the right call here.


Solution

  • Try this...

    string infoText = myIE.Div(Find.ByClass("doc-banner-icon")).Images.First().Src;
    string imageText = myIE.Div(Find.ById("doc-original-text")).Text;