How to iterate DOM elements in selenium using java?

On twitter, one can post a tweet as a thread: enter image description here

HTML- DOM :

<div aria-activedescendant="typeaheadFocus-0.9544887314365358" aria-autocomplete="list" aria-controls="typeaheadDropdownWrapped-11" aria-describedby="placeholder-1q1t4" aria-label="Tweet text" aria-multiline="true" class="notranslate public-DraftEditor-content" contenteditable="true" data-testid="tweetTextarea_8" role="textbox" spellcheck="true" tabindex="0" no-focuscontainer-refocus="true" style="outline: none; user-select: text; white-space: pre-wrap; overflow-wrap: break-word;"><div data-contents="true"><div class="" data-block="true" data-editor="1q1t4" data-offset-key="e95u3-0-0"><div data-offset-key="e95u3-0-0" class="public-DraftStyleDefault-block public-DraftStyleDefault-ltr"><span style="color: rgb(27, 149, 224);"><span data-offset-key="e95u3-0-0"><span data-text="true">#Test</span></span></span></div></div></div></div>

My selenium code :

driver.findElement(By.xpath("//a[@data-testid='addButton']")).click();
driver.findElement(By.xpath("//div[@data-testid='tweetTextarea_1']")).sendKeys(tag);
//for (int i = 0; i < 5; i++){
        driver.findElement(By.xpath("//div[@aria-label='Add Tweet']")).click();
        driver.findElement(By.xpath("//div[@data-testid='tweetTextarea_2']")).sendKeys(tag);
        driver.findElement(By.xpath("//div[@aria-label='Add Tweet']")).click();
        driver.findElement(By.xpath("//div[@data-testid='tweetTextarea_3']")).sendKeys(tag);
        driver.findElement(By.xpath("//div[@aria-label='Add Tweet']")).click();
        driver.findElement(By.xpath("//div[@data-testid='tweetTextarea_4']")).sendKeys(tag);
        driver.findElement(By.xpath("//div[@aria-label='Add Tweet']")).click();
        driver.findElement(By.xpath("//div[@data-testid='tweetTextarea_5']")).sendKeys(tag);
        driver.findElement(By.xpath("//div[@aria-label='Add Tweet']")).click();

        driver.findElement(By.xpath("//div[@data-testid='tweetTextarea_6']")).sendKeys(tag);
        driver.findElement(By.xpath("//div[@aria-label='Add Tweet']")).click();
        driver.findElement(By.xpath("//div[@data-testid='tweetTextarea_7']")).sendKeys(tag);
        driver.findElement(By.xpath("//div[@aria-label='Add Tweet']")).click();
        driver.findElement(By.xpath("//div[@data-testid='tweetTextarea_8']")).sendKeys(tag);
        driver.findElement(By.xpath("//div[@aria-label='Add Tweet']")).click();
        driver.findElement(By.xpath("//div[@data-testid='tweetTextarea_9']")).sendKeys(tag);
        driver.findElement(By.xpath("//div[@aria-label='Add Tweet']")).click();
        driver.findElement(By.xpath("//div[@data-testid='tweetTextarea_10']")).sendKeys(tag);

Answer

What I would suggest here is to have a list of strings, that will be your input string, basically tweet.

List<String> inputTweet = new ArrayList<String>(Arrays.asList("Tweet1", "Tweet2", "Tweet3"));

and then target the text area, also if you pay attention, tweetTextarea_2 is getting incremented. so that you can increment also in your program.

Code :-

 driver.findElement(By.xpath("//div[@aria-label='Add Tweet']")).click();
 int textArea = 2;
 for (int i = 0; i<inputTweet.size(); i++) {
     Thread.sleep(1000);
     driver.findElement(By.xpath("//div[@data-testid='tweetTextarea_'"+textArea+"']")).sendKeys(inputTweet.get(i));
     textArea++;     
 }