1

I am automating a web page. i have captured and saved the Links in a file.

Link Url_0="gmail.com"
Link Url_1="ymail.com"
Link Url_2="hotmail.com"
Link Url_3="outlook.com"

The below statement will click on each url.

HomePage.Url_0.Click();//Homepage is the Class name

I want to Click these URLs one by one. So I am using a for loop.

for(int i=0;i<3;i++)
{
String url=String.Format("Url_{0}",i);
HomePage.url.Click(); //This is throwing me error (I think that this is not correct way to do.)
Sleep(2000);
}

How can I proceed here ? Can this be done in any way ? Any help is appreciated.

1
  • 5
    You seriously need to read about arrays! Commented Dec 4, 2012 at 14:52

2 Answers 2

4

You should put the variables into a collection, rather than having a different variable with a different name for each. It's technically possible to access the variable in the manor you describe, but it's not what a language like C# is designed to do, and would be very bad practice.

There are several collections to choose from. Here a List is probably appropriate, an array could work as well.

List<string> urls = new List<string>()
{
    "gmail.com",
    "ymail.com",
    "hotmail.com",
    "outlook.com"
};

foreach (string url in urls)
{
    //do whatever with the url
    Console.WriteLine(url);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Of course, List<string> is not an array - it's a list of strings.
@tomfanning It's a wrapper around an array. You can think of it as pretty much just an array, but you're right, I'll edit it.
0

You can store all links you want into a Coolection of type: IList<Link> or into an IEnumerable<Link>

IList<Link> myCollection = new List<Link>();

After that, you'll go throuh items in the collection with an

foreach(var item in myCollection ) {
      //Here implement your logic with click
}

2 Comments

You can't new an IList.
@Servy - yes .. I miss that. Shouls be IList<Link> myCollection = new List<Link>(); Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.