title: Iterate through elements and retrieve text values
body: <p>I have a small scraping program, that decides how many times to loop correctly</p>
  <p>I have three values i want extracted. &quot;// GET THREE VALUES&quot;</p>
  <p>I want to put a counter on them , so each loop will get the value for each loop.</p>
  <p>Looking for a better way of doing it , currently it only gets first value over and over... until end of loop.</p>
  <pre><code>package scraping;
  
  import java.util.List;
  import java.util.concurrent.TimeUnit;
  import org.openqa.selenium.By;
  import org.openqa.selenium.WebDriver;
  import org.openqa.selenium.WebElement;
  import org.openqa.selenium.chrome.ChromeDriver;
  
  import org.testng.annotations.AfterClass;
  import org.testng.annotations.BeforeClass;
  import org.testng.annotations.Test;
  
  import java.io.IOException;
  
  public class scrape {
  
      private WebDriver driver;
  
      @BeforeClass
      public void beforeClass() {
  
          System.setProperty(&quot;webdriver.chrome.driver&quot;, &quot;C:\\Selenium\\ChromeDriver\\chromedriver.exe&quot;);
  
          driver = new ChromeDriver();
  
      }
  
      @Test
      public void writeexcel() throws InterruptedException, IOException {
  
          driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  
          driver.get(
                  &quot;https://www.thephonebook.bt.com/Business/TypeSearch/?BusinessSearchTerm=farmers&amp;Location=Bedford%20%20(Bedfordshire)&amp;PageNumber=1&amp;LocationFound=true&quot;);
  
          List&lt;WebElement&gt; elements = driver.findElements(By.xpath(&quot;//span[contains(@class,'black medium pr-4')]&quot;));
  
          for (int i = 1; i &lt; 26; i = i + 2) {
  
              System.out.println(&quot;count = &quot; + i);
  
                  // GET THREE VALUES
      
              WebElement name = driver.findElement(By.xpath(&quot;//span[contains(@class,'black medium pr-4')]&quot;));
              String Nameval = name.getAttribute(&quot;innerHTML&quot;);
  
              WebElement Add1 = driver.findElement(By.xpath(&quot;//div[contains(@class,'description truncatePL linktooltipAddressListing')]&quot;));
              String Add1val = Add1.getAttribute(&quot;innerHTML&quot;);
              
              WebElement Tel = driver.findElement(By.xpath(&quot;//a[contains(@data-ref,'phone number')]&quot;));
              String Telval = Tel.getAttribute(&quot;innerHTML&quot;);
  
  
              System.out.println(Nameval + &quot; , &quot; + Add1val + &quot; , &quot; + Telval);
  
          }
      }
  
      @AfterClass
      public void afterClass() {
          driver.quit();
      }
  
  }```
  </code></pre>