Show Contents / Index / Search

Get a Web Object

//This sample code is intended to illustrate a concept, and it shows only the code that is relevant to that concept.

//It may not meet the security requirements for a specific environment, and it should not be used exactly as shown.

//We recommend that you add security and error-handling code to make your projects more secure and robust.

//Attachmate provides this sample code "AS IS" with no warranties.

 

using System;

using System.Collections.Generic;

using System.Text;

using Attachmate.Reflection.Web.Msie;

using Attachmate.Reflection.Framework;

 

//Before you run GetTextFromWebPage, follow the instructions in Get Text From a Web Page

//to create the Webtable.urlx document.

namespace GetTextFromWebPage

{

  class Program

  {

    static void Main(string[] args)

    {

      Attachmate.Reflection.Framework.Application reflectionApplication = MyReflection.CreateApplication("workspace", true);

      if (reflectionApplication != null)

      {

        //Create the Web control.

        IWebControl webControl = (IWebControl)reflectionApplication.CreateControl(@"C:\Users\[user name]\Documents\Attachmate\Reflection\Webtable.urlx");

        if (webControl != null)

        {

          IWebDocument webDocument = webControl.Document;

          if (webDocument != null)

          {

            //Get the Web element by using the xpath.

            IWebElement webElement = webDocument.GetElement("HTML/BODY/TABLE/TBODY/TR[1]/TD[1]");

            if (webElement != null)

            {

              string conversionRate = webElement.InnerText;

              Console.WriteLine("The currency conversion rate from Canadian to U.S. dollars: " + conversionRate);

            }

            else

              Console.WriteLine("Can not find the webElement. Please check the xpath for the webElement.");

          }

          else

            Console.WriteLine("Fatal error. Check the Reflection 2011 installation.");

        }

        else

          Console.WriteLine("Can not create the Web control. Check the Web session file path.");

      }

      else

        Console.WriteLine("Failed to get Application object. Check the Reflection 2011 installation and the API port number.");

    }

  }

}