Sunday, April 17, 2011

Using WatiN, how can I access the head part of the page, specifically the meta tags?

WatIn provides great functionality for programmatic access to the displayed parts of a Web page.

I want to access the head part of the page, spedifically the META tags. Watin allows me access to the TITLE, but AFAICT nothing else. There is an InternetExplorer property which allows access to ShDocVw.InternetExplorer. I suspect this might be the start of the path. Even if it is the right path, I don't know how to follow it.

From stackoverflow
  • browser
        .Element(Find.ByName(nameAttribute))
        .GetAttributeValue("content");
    
    RichardHowells : Thanks - this got me to ie.Element(Find.ByName("KEYWORDS")).GetAttributeValue("content"); I also found success with ie.Elements.Where(el => el.Id == "MetaKeywords")
    eglasius : y, if you also have the id set to MetaKeywords (as your second way points reflects) you could just: ie.Element("MetaKeywords") :)
  • Hi Richard,

    This will give you a collection of meta tags in your page.

    Syntax in WatiN 2.0 beta 1:

    var metaTags = browser.ElementsWithTag("meta");
    

    Syntax in WatiN 2.0 CTPs and earlier:

    var metaTags  = browser.Elements.Filter(Find.By("tagName", "META"));
    

    If you prever the following syntax, read my blog post about adding elements to WatiN:

    var metaTags = browser.ElementsOfType<Meta>();
    
    RichardHowells : Hi - I could not get this to compile on my WatiN install (1.3.0) - Elements is just a property for me.
    Jeroen van Menen : Sorry, I have corrected the example

0 comments:

Post a Comment