title: Iterating through a list of links in a table using Selenium - First iteration works fine and fails afterwards
body: <p>This works fine: </p>
  
  <pre><code>driver.findElement(By.xpath(".//table[@class='list']//td[normalize-space()='Edit']/following-sibling::th/a")).click();
  </code></pre>
  
  <p>But fails when I iterate it for a table like this:</p>
  
  <p><code>row.findElement(By.xpath(".//table[@class='list']//td[normalize-space()='Edit']/following-sibling::th/a")).click();</code></p>
  
  <p><strong>The HTML</strong>:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;table cellspacing="0" cellpadding="0" border="0" class="list"&gt;
    &lt;tbody&gt;
      &lt;tr class="headerRow"&gt;
        &lt;th scope="col" class="actionColumn"&gt;Action&lt;/th&gt;
        &lt;th class=" zen-deemphasize" scope="col"&gt;&lt;a title="Customer Name - Click to sort ascending" onclick="window.sfdcPage.wrapDhtmlWaitingRequest(null,'/_ui/search/ui/UnifiedSearchResults?offset=0&amp;amp;fpg=-1kpv5z603z3zv&amp;amp;str=edwin&amp;amp;sen=001&amp;amp;sen=003&amp;amp;sen=005&amp;amp;sen=a0H&amp;amp;sen=a08&amp;amp;sen=a07&amp;amp;sen=a0I&amp;amp;initialViewMode=summary','/_ui/search/ui/UnifiedSearchResults?offset=0&amp;amp;fpg=-1kpv5z603z3zv&amp;amp;str=edwin&amp;amp;sen=001&amp;amp;sen=003&amp;amp;sen=005&amp;amp;sen=a0H&amp;amp;sen=a08&amp;amp;sen=a07&amp;amp;sen=a0I&amp;amp;initialViewMode=summary&amp;amp;srtClmn=ACCOUNT.NAME&amp;amp;srtOrd=1&amp;amp;relatedListId=Account','noh=1','Account','Sorting...');return false;"
            href=""&gt;Customer Name&lt;/a&gt;&lt;/th&gt;
        &lt;th class=" zen-deemphasize" scope="col"&gt;&lt;a title="Customer Site - Click to sort ascending" onclick="window.sfdcPage.wrapDhtmlWaitingRequest(null,'/_ui/search/ui/UnifiedSearchResults?offset=0&amp;amp;fpg=-1kpv5z603z3zv&amp;amp;str=edwin&amp;amp;sen=001&amp;amp;sen=003&amp;amp;sen=005&amp;amp;sen=a0H&amp;amp;sen=a08&amp;amp;sen=a07&amp;amp;sen=a0I&amp;amp;initialViewMode=summary','/_ui/search/ui/UnifiedSearchResults?offset=0&amp;amp;fpg=-1kpv5z603z3zv&amp;amp;str=edwin&amp;amp;sen=001&amp;amp;sen=003&amp;amp;sen=005&amp;amp;sen=a0H&amp;amp;sen=a08&amp;amp;sen=a07&amp;amp;sen=a0I&amp;amp;initialViewMode=summary&amp;amp;srtClmn=ACCOUNT.SITE&amp;amp;srtOrd=1&amp;amp;relatedListId=Account','noh=1','Account','Sorting...');return false;"
            href=""&gt;Customer Site&lt;/a&gt;&lt;/th&gt;
        &lt;th class=" zen-deemphasize" scope="col"&gt;&lt;a title="Work Phone - Click to sort ascending" onclick="window.sfdcPage.wrapDhtmlWaitingRequest(null,'/_ui/search/ui/UnifiedSearchResults?offset=0&amp;amp;fpg=-1kpv5z603z3zv&amp;amp;str=edwin&amp;amp;sen=001&amp;amp;sen=003&amp;amp;sen=005&amp;amp;sen=a0H&amp;amp;sen=a08&amp;amp;sen=a07&amp;amp;sen=a0I&amp;amp;initialViewMode=summary','/_ui/search/ui/UnifiedSearchResults?offset=0&amp;amp;fpg=-1kpv5z603z3zv&amp;amp;str=edwin&amp;amp;sen=001&amp;amp;sen=003&amp;amp;sen=005&amp;amp;sen=a0H&amp;amp;sen=a08&amp;amp;sen=a07&amp;amp;sen=a0I&amp;amp;initialViewMode=summary&amp;amp;srtClmn=00NE0000004a58n&amp;amp;srtOrd=1&amp;amp;relatedListId=Account','noh=1','Account','Sorting...');return false;"
            href=""&gt;Work Phone&lt;/a&gt;&lt;/th&gt;
        &lt;th class=" zen-deemphasize" scope="col"&gt;&lt;a title="Customer Owner Alias - Click to sort ascending" onclick="window.sfdcPage.wrapDhtmlWaitingRequest(null,'/_ui/search/ui/UnifiedSearchResults?offset=0&amp;amp;fpg=-1kpv5z603z3zv&amp;amp;str=edwin&amp;amp;sen=001&amp;amp;sen=003&amp;amp;sen=005&amp;amp;sen=a0H&amp;amp;sen=a08&amp;amp;sen=a07&amp;amp;sen=a0I&amp;amp;initialViewMode=summary','/_ui/search/ui/UnifiedSearchResults?offset=0&amp;amp;fpg=-1kpv5z603z3zv&amp;amp;str=edwin&amp;amp;sen=001&amp;amp;sen=003&amp;amp;sen=005&amp;amp;sen=a0H&amp;amp;sen=a08&amp;amp;sen=a07&amp;amp;sen=a0I&amp;amp;initialViewMode=summary&amp;amp;srtClmn=CORE.USERS.ALIAS&amp;amp;srtOrd=1&amp;amp;relatedListId=Account','noh=1','Account','Sorting...');return false;"
            href=""&gt;Customer Owner Alias&lt;/a&gt;&lt;/th&gt;
      &lt;/tr&gt;
  
      &lt;!-- ListRow --&gt;
      &lt;tr onmouseover="if (window.hiOn){hiOn(this);}" onmouseout="if (window.hiOff){hiOff(this);}" onfocus="if (window.hiOn){hiOn(this);}" onblur="if (window.hiOff){hiOff(this);}" class="dataRow even first"&gt;
        &lt;td class="actionColumn"&gt;&lt;a title="Edit - Record 1 - NELSON,EDWIN" onmousedown="searchResultClick.mousedown(this, event)" data-seclkr="1" data-seclkp="/001Q0000012jqBq/e" data-seclki="001Q0000012jqBq" data-seclkh="fb70693f5a4ab349f7bf6e74c4f8acff" data-seclke="Account"
            class="actionLink" href="/001Q0000012jqBq/e?srPos=0&amp;amp;srKp=001&amp;amp;retURL=%2F_ui%2Fsearch%2Fui%2FUnifiedSearchResults%3F_dc%3D1492761117569%26searchCount%3D1%26cmdType%3Dcmp%26cmp%3DsummarySearchResult%26cmp%3DsidetabAJAX%26str%3Dedwin%26initialViewMode%3Dsummary%26sen%3D001%26sen%3D003%26sen%3D005%26sen%3Da0H%26sen%3Da08%26sen%3Da07%26sen%3Da0I%26fpg%3D-1kpv5z603z3zv"&gt;Edit&lt;/a&gt;&lt;/td&gt;
        &lt;th
          class=" dataCell  " scope="row"&gt;&lt;a onmousedown="searchResultClick.mousedown(this, event)" data-seclkr="1" data-seclkp="/001Q0000012jqBq" data-seclki="001Q0000012jqBq" data-seclkh="fb70693f5a4ab349f7bf6e74c4f8acff" data-seclke="Account" href="/001Q0000012jqBq?srPos=0&amp;amp;srKp=001"&gt;NELSON,EDWIN&lt;/a&gt;&lt;/th&gt;
          &lt;td
            class=" dataCell  "&gt;&amp;nbsp;&lt;/td&gt;
            &lt;td class=" dataCell  "&gt;(444) 444-4444&lt;/td&gt;
            &lt;td class=" dataCell  "&gt;&lt;a onmousedown="searchResultClick.mousedown(this, event)" data-seclkr="1" data-seclkp="/005E0000005pAqf" data-seclki="001Q0000012jqBq" data-seclkh="fb70693f5a4ab349f7bf6e74c4f8acff" data-seclke="Account" href="/005E0000005pAqf?srPos=0&amp;amp;srKp=001"&gt;webapi&lt;/a&gt;&lt;/td&gt;
      &lt;/tr&gt;
      &lt;!-- ListRow --&gt;
      &lt;tr onmouseover="if (window.hiOn){hiOn(this);}" onmouseout="if (window.hiOff){hiOff(this);}" onfocus="if (window.hiOn){hiOn(this);}" onblur="if (window.hiOff){hiOff(this);}" class="dataRow odd"&gt;
        &lt;td class="actionColumn"&gt;&lt;a title="Edit - Record 2 - NELSON,EDWIN" onmousedown="searchResultClick.mousedown(this, event)" data-seclkr="2" data-seclkp="/001Q0000013hzA7/e" data-seclki="001Q0000013hzA7" data-seclkh="b182c705723f5e1a6f828808c22c20f3" data-seclke="Account"
            class="actionLink" href="/001Q0000013hzA7/e?srPos=1&amp;amp;srKp=001&amp;amp;retURL=%2F_ui%2Fsearch%2Fui%2FUnifiedSearchResults%3F_dc%3D1492761117569%26searchCount%3D1%26cmdType%3Dcmp%26cmp%3DsummarySearchResult%26cmp%3DsidetabAJAX%26str%3Dedwin%26initialViewMode%3Dsummary%26sen%3D001%26sen%3D003%26sen%3D005%26sen%3Da0H%26sen%3Da08%26sen%3Da07%26sen%3Da0I%26fpg%3D-1kpv5z603z3zv"&gt;Edit&lt;/a&gt;&lt;/td&gt;
        &lt;th
          class=" dataCell  " scope="row"&gt;&lt;a onmousedown="searchResultClick.mousedown(this, event)" data-seclkr="2" data-seclkp="/001Q0000013hzA7" data-seclki="001Q0000013hzA7" data-seclkh="b182c705723f5e1a6f828808c22c20f3" data-seclke="Account" href="/001Q0000013hzA7?srPos=1&amp;amp;srKp=001"&gt;NELSON,EDWIN&lt;/a&gt;&lt;/th&gt;
          &lt;td
            class=" dataCell  "&gt;&amp;nbsp;&lt;/td&gt;
            &lt;td class=" dataCell  "&gt;(435) 345-4543&lt;/td&gt;
            &lt;td class=" dataCell  "&gt;&lt;a onmousedown="searchResultClick.mousedown(this, event)" data-seclkr="2" data-seclkp="/005E0000005pAqf" data-seclki="001Q0000013hzA7" data-seclkh="b182c705723f5e1a6f828808c22c20f3" data-seclke="Account" href="/005E0000005pAqf?srPos=1&amp;amp;srKp=001"&gt;webapi&lt;/a&gt;&lt;/td&gt;
      &lt;/tr&gt;
      &lt;!-- ListRow --&gt;
      &lt;tr onmouseover="if (window.hiOn){hiOn(this);}" onmouseout="if (window.hiOff){hiOff(this);}" onfocus="if (window.hiOn){hiOn(this);}" onblur="if (window.hiOff){hiOff(this);}" class="dataRow even"&gt;
        &lt;td class="actionColumn"&gt;&lt;a title="Edit - Record 3 - NELSON,EDWIN" onmousedown="searchResultClick.mousedown(this, event)" data-seclkr="3" data-seclkp="/001Q0000014lLNc/e" data-seclki="001Q0000014lLNc" data-seclkh="36ddf075be1199f1439c154f56aa01ee" data-seclke="Account"
            class="actionLink" href="/001Q0000014lLNc/e?srPos=2&amp;amp;srKp=001&amp;amp;retURL=%2F_ui%2Fsearch%2Fui%2FUnifiedSearchResults%3F_dc%3D1492761117569%26searchCount%3D1%26cmdType%3Dcmp%26cmp%3DsummarySearchResult%26cmp%3DsidetabAJAX%26str%3Dedwin%26initialViewMode%3Dsummary%26sen%3D001%26sen%3D003%26sen%3D005%26sen%3Da0H%26sen%3Da08%26sen%3Da07%26sen%3Da0I%26fpg%3D-1kpv5z603z3zv"&gt;Edit&lt;/a&gt;&lt;/td&gt;
        &lt;th
          class=" dataCell  " scope="row"&gt;&lt;a onmousedown="searchResultClick.mousedown(this, event)" data-seclkr="3" data-seclkp="/001Q0000014lLNc" data-seclki="001Q0000014lLNc" data-seclkh="36ddf075be1199f1439c154f56aa01ee" data-seclke="Account" href="/001Q0000014lLNc?srPos=2&amp;amp;srKp=001"&gt;NELSON,EDWIN&lt;/a&gt;&lt;/th&gt;
          &lt;td
            class=" dataCell  "&gt;&amp;nbsp;&lt;/td&gt;
            &lt;td class=" dataCell  "&gt;(444) 444-4444&lt;/td&gt;
            &lt;td class=" dataCell  "&gt;&lt;a onmousedown="searchResultClick.mousedown(this, event)" data-seclkr="3" data-seclkp="/005E0000005pAqf" data-seclki="001Q0000014lLNc" data-seclkh="36ddf075be1199f1439c154f56aa01ee" data-seclke="Account" href="/005E0000005pAqf?srPos=2&amp;amp;srKp=001"&gt;webapi&lt;/a&gt;&lt;/td&gt;
      &lt;/tr&gt;
      &lt;!-- ListRow --&gt;
      &lt;tr onmouseover="if (window.hiOn){hiOn(this);}" onmouseout="if (window.hiOff){hiOff(this);}" onfocus="if (window.hiOn){hiOn(this);}" onblur="if (window.hiOff){hiOff(this);}" class="dataRow odd"&gt;
        &lt;td class="actionColumn"&gt;&lt;a title="Edit - Record 4 - Edwin,Nelson" onmousedown="searchResultClick.mousedown(this, event)" data-seclkr="4" data-seclkp="/001Q0000014jyLD/e" data-seclki="001Q0000014jyLD" data-seclkh="ebc0d5e11b734a7961e526f4db9f4ed1" data-seclke="Account"
            class="actionLink" href="/001Q0000014jyLD/e?srPos=3&amp;amp;srKp=001&amp;amp;retURL=%2F_ui%2Fsearch%2Fui%2FUnifiedSearchResults%3F_dc%3D1492761117569%26searchCount%3D1%26cmdType%3Dcmp%26cmp%3DsummarySearchResult%26cmp%3DsidetabAJAX%26str%3Dedwin%26initialViewMode%3Dsummary%26sen%3D001%26sen%3D003%26sen%3D005%26sen%3Da0H%26sen%3Da08%26sen%3Da07%26sen%3Da0I%26fpg%3D-1kpv5z603z3zv"&gt;Edit&lt;/a&gt;&lt;/td&gt;
        &lt;th
          class=" dataCell  " scope="row"&gt;&lt;a onmousedown="searchResultClick.mousedown(this, event)" data-seclkr="4" data-seclkp="/001Q0000014jyLD" data-seclki="001Q0000014jyLD" data-seclkh="ebc0d5e11b734a7961e526f4db9f4ed1" data-seclke="Account" href="/001Q0000014jyLD?srPos=3&amp;amp;srKp=001"&gt;Edwin,Nelson&lt;/a&gt;&lt;/th&gt;
          &lt;td
            class=" dataCell  "&gt;&amp;nbsp;&lt;/td&gt;
            &lt;td class=" dataCell  "&gt;(353) 242-3423&lt;/td&gt;
            &lt;td class=" dataCell  "&gt;&lt;a onmousedown="searchResultClick.mousedown(this, event)" data-seclkr="4" data-seclkp="/005E0000005pAqf" data-seclki="001Q0000014jyLD" data-seclkh="ebc0d5e11b734a7961e526f4db9f4ed1" data-seclke="Account" href="/005E0000005pAqf?srPos=3&amp;amp;srKp=001"&gt;webapi&lt;/a&gt;&lt;/td&gt;
      &lt;/tr&gt;
      &lt;!-- ListRow --&gt;
      &lt;tr onmouseover="if (window.hiOn){hiOn(this);}" onmouseout="if (window.hiOff){hiOff(this);}" onfocus="if (window.hiOn){hiOn(this);}" onblur="if (window.hiOff){hiOff(this);}" class="dataRow even last"&gt;
        &lt;td class="actionColumn"&gt;&lt;a title="Edit - Record 5 - NELSON,EDWIN" onmousedown="searchResultClick.mousedown(this, event)" data-seclkr="5" data-seclkp="/001Q0000012m1Xb/e" data-seclki="001Q0000012m1Xb" data-seclkh="4226e6544070ec8c55fdc3fec27f099c" data-seclke="Account"
            class="actionLink" href="/001Q0000012m1Xb/e?srPos=4&amp;amp;srKp=001&amp;amp;retURL=%2F_ui%2Fsearch%2Fui%2FUnifiedSearchResults%3F_dc%3D1492761117569%26searchCount%3D1%26cmdType%3Dcmp%26cmp%3DsummarySearchResult%26cmp%3DsidetabAJAX%26str%3Dedwin%26initialViewMode%3Dsummary%26sen%3D001%26sen%3D003%26sen%3D005%26sen%3Da0H%26sen%3Da08%26sen%3Da07%26sen%3Da0I%26fpg%3D-1kpv5z603z3zv"&gt;Edit&lt;/a&gt;&lt;/td&gt;
        &lt;th
          class=" dataCell  " scope="row"&gt;&lt;a onmousedown="searchResultClick.mousedown(this, event)" data-seclkr="5" data-seclkp="/001Q0000012m1Xb" data-seclki="001Q0000012m1Xb" data-seclkh="4226e6544070ec8c55fdc3fec27f099c" data-seclke="Account" href="/001Q0000012m1Xb?srPos=4&amp;amp;srKp=001"&gt;NELSON,EDWIN&lt;/a&gt;&lt;/th&gt;
          &lt;td
            class=" dataCell  "&gt;&amp;nbsp;&lt;/td&gt;
            &lt;td class=" dataCell  "&gt;(656) 757-5463&lt;/td&gt;
            &lt;td class=" dataCell  "&gt;&lt;a onmousedown="searchResultClick.mousedown(this, event)" data-seclkr="5" data-seclkp="/005E0000005pAqf" data-seclki="001Q0000012m1Xb" data-seclkh="4226e6544070ec8c55fdc3fec27f099c" data-seclke="Account" href="/005E0000005pAqf?srPos=4&amp;amp;srKp=001"&gt;webapi&lt;/a&gt;&lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;
  &lt;/table&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p><strong>Java  code:</strong>
  <div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>          // Grab the table
            WebElement table = driver.findElement(By.xpath("/html/body/div[1]/div[2]/table/tbody/tr/td/div[2]/table/tbody/tr/td[2]/div[2]/div[2]/div[1]/div/div[2]/div/div[2]/table"));
           System.out.println("table is selected");
          // Now get all the TR elements from the table
          List&lt;WebElement&gt; allRows = table.findElements(By.tagName("tr"));
          System.out.println("row is selected");
          // And iterate over them, getting the cells
          for (WebElement row : allRows) 
          {
           System.out.println("in first loop");
           row.findElement(By.xpath(".//table[@class='list']//td[normalize-space()='Edit']/following-sibling::th/a")).click();//This is where am facing problem. The element has to be clicked for each row every iteration
     
                  System.out.println("element");
      
         driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
         scrollToApplications();
             driver.navigate().back();</code></pre>
  </div>
  </div>
  </p>
  
  <p>Have no clue where I am going wrong.<br/>
  Any help would be appreciated. <br/>
  Thanks</p>