简体   繁体   中英

How to add more iterations in a for loop C# Make my loop pick up more

I am trying to show multiple things in a textbox in my software and its just printing out 1 / 5

I think I got the for loop all mixed up so here is how it looks

var getTopFive = new FirefoxDriver();
getTopFive.Navigate().GoToUrl("https://www.tradingview.com/");

IList<IWebElement> movies = getTopFive.FindElements(By.CssSelector("tbody tr"));

for (int i = 0; i < 1; ++i)
{
    activeTextBox.Text = movies[i].Text;  
}

This is what I tried and failed with.

I tried adding another

activeTextBox.Text = movies[i].Text;

Which did not work

I also tried adding a second block of the same code showed at the top but with a different name for the int etc.

Then I tried adding a else if below it which gave me another error.

So my question is, how do I make the loop go through all the 5 items instead of just showing the first one which it does by this line of code right here.

for (int i = 0; i < 1; ++i)
{
  activeTextBox.Text = movies[i].Text;
}

There are two issues here. The first is your for loop is only going to happen once because for(int i = 0; i < 1; i++) .

To loop more than once, you need to change to 1 to something else. If you want it dynamic, use for(int i = 0; i < movies.Count; i++) .

The second issue is activeTextBox.Text is being over-written each time you write to it. No matter how many times you repeat the loop or the line, the only thing that text box will show is the last item in the loop.

If you want it to show all items, you need to do something like: activeTextBox.Text += movies[i].Text + "\\n";

The \\n will put each movie on a separate line - you could use a hyphen or similar to separate each item.

IList has a property called Count . This property returns the number of elements in the list. Use it in your loop like this to show all movies:

for (int i = 0; i < movies.Count; i++)
{
  activeTextBox.Text = movies[i].Text;
}

A second way is to use a foreach loop like this:

foreach (IWebElement WebElement in movies)
{
  activeTextBox.Text = WebElement.Text;
}

Remember you are overwriting your text each loop. So in the end your text will be the last item of your loop. If you want to add the names behind each other do it like this (seperated with a minus)

activeTextBox.Text += " - " + movies[i].Text;

or this

activeTextBox.Text += " - " + WebElement.Text

clear the Text before your loop with

activeTextBox.Text.Clear();

Try using this code (don't forget to add using System.Linq; ):

activeTextBox.Text = string.Join("; ", movies.Select(x => x.Text));

Explanation:

movies.Select(x => x.Text) takes every IWebElement (movie) and return its Text property. So you end up with a list of movie names instead of IWebElement objects.

Next, you join those movies together into a single string, separated by semicolon. ( string.Join("; ", ...) )

And then you assign the result to the activeTextBox.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM