Cela fonctionne bien:

driver.findElement(By.xpath(".//table[@class='list']//td[normalize-space()='Edit']/following-sibling::th/a")).click();

Mais échoue lorsque je l'itère pour une table comme celle-ci:

row.findElement(By.xpath(".//table[@class='list']//td[normalize-space()='Edit']/following-sibling::th/a")).click();

Le HTML :

<table cellspacing="0" cellpadding="0" border="0" class="list">
  <tbody>
    <tr class="headerRow">
      <th scope="col" class="actionColumn">Action</th>
      <th class=" zen-deemphasize" scope="col"><a title="Customer Name - Click to sort ascending" onclick="window.sfdcPage.wrapDhtmlWaitingRequest(null,'/_ui/search/ui/UnifiedSearchResults?offset=0&amp;fpg=-1kpv5z603z3zv&amp;str=edwin&amp;sen=001&amp;sen=003&amp;sen=005&amp;sen=a0H&amp;sen=a08&amp;sen=a07&amp;sen=a0I&amp;initialViewMode=summary','/_ui/search/ui/UnifiedSearchResults?offset=0&amp;fpg=-1kpv5z603z3zv&amp;str=edwin&amp;sen=001&amp;sen=003&amp;sen=005&amp;sen=a0H&amp;sen=a08&amp;sen=a07&amp;sen=a0I&amp;initialViewMode=summary&amp;srtClmn=ACCOUNT.NAME&amp;srtOrd=1&amp;relatedListId=Account','noh=1','Account','Sorting...');return false;"
          href="">Customer Name</a></th>
      <th class=" zen-deemphasize" scope="col"><a title="Customer Site - Click to sort ascending" onclick="window.sfdcPage.wrapDhtmlWaitingRequest(null,'/_ui/search/ui/UnifiedSearchResults?offset=0&amp;fpg=-1kpv5z603z3zv&amp;str=edwin&amp;sen=001&amp;sen=003&amp;sen=005&amp;sen=a0H&amp;sen=a08&amp;sen=a07&amp;sen=a0I&amp;initialViewMode=summary','/_ui/search/ui/UnifiedSearchResults?offset=0&amp;fpg=-1kpv5z603z3zv&amp;str=edwin&amp;sen=001&amp;sen=003&amp;sen=005&amp;sen=a0H&amp;sen=a08&amp;sen=a07&amp;sen=a0I&amp;initialViewMode=summary&amp;srtClmn=ACCOUNT.SITE&amp;srtOrd=1&amp;relatedListId=Account','noh=1','Account','Sorting...');return false;"
          href="">Customer Site</a></th>
      <th class=" zen-deemphasize" scope="col"><a title="Work Phone - Click to sort ascending" onclick="window.sfdcPage.wrapDhtmlWaitingRequest(null,'/_ui/search/ui/UnifiedSearchResults?offset=0&amp;fpg=-1kpv5z603z3zv&amp;str=edwin&amp;sen=001&amp;sen=003&amp;sen=005&amp;sen=a0H&amp;sen=a08&amp;sen=a07&amp;sen=a0I&amp;initialViewMode=summary','/_ui/search/ui/UnifiedSearchResults?offset=0&amp;fpg=-1kpv5z603z3zv&amp;str=edwin&amp;sen=001&amp;sen=003&amp;sen=005&amp;sen=a0H&amp;sen=a08&amp;sen=a07&amp;sen=a0I&amp;initialViewMode=summary&amp;srtClmn=00NE0000004a58n&amp;srtOrd=1&amp;relatedListId=Account','noh=1','Account','Sorting...');return false;"
          href="">Work Phone</a></th>
      <th class=" zen-deemphasize" scope="col"><a title="Customer Owner Alias - Click to sort ascending" onclick="window.sfdcPage.wrapDhtmlWaitingRequest(null,'/_ui/search/ui/UnifiedSearchResults?offset=0&amp;fpg=-1kpv5z603z3zv&amp;str=edwin&amp;sen=001&amp;sen=003&amp;sen=005&amp;sen=a0H&amp;sen=a08&amp;sen=a07&amp;sen=a0I&amp;initialViewMode=summary','/_ui/search/ui/UnifiedSearchResults?offset=0&amp;fpg=-1kpv5z603z3zv&amp;str=edwin&amp;sen=001&amp;sen=003&amp;sen=005&amp;sen=a0H&amp;sen=a08&amp;sen=a07&amp;sen=a0I&amp;initialViewMode=summary&amp;srtClmn=CORE.USERS.ALIAS&amp;srtOrd=1&amp;relatedListId=Account','noh=1','Account','Sorting...');return false;"
          href="">Customer Owner Alias</a></th>
    </tr>

    <!-- ListRow -->
    <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">
      <td class="actionColumn"><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;srKp=001&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">Edit</a></td>
      <th
        class=" dataCell  " scope="row"><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;srKp=001">NELSON,EDWIN</a></th>
        <td
          class=" dataCell  ">&nbsp;</td>
          <td class=" dataCell  ">(444) 444-4444</td>
          <td class=" dataCell  "><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;srKp=001">webapi</a></td>
    </tr>
    <!-- ListRow -->
    <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">
      <td class="actionColumn"><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;srKp=001&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">Edit</a></td>
      <th
        class=" dataCell  " scope="row"><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;srKp=001">NELSON,EDWIN</a></th>
        <td
          class=" dataCell  ">&nbsp;</td>
          <td class=" dataCell  ">(435) 345-4543</td>
          <td class=" dataCell  "><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;srKp=001">webapi</a></td>
    </tr>
    <!-- ListRow -->
    <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">
      <td class="actionColumn"><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;srKp=001&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">Edit</a></td>
      <th
        class=" dataCell  " scope="row"><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;srKp=001">NELSON,EDWIN</a></th>
        <td
          class=" dataCell  ">&nbsp;</td>
          <td class=" dataCell  ">(444) 444-4444</td>
          <td class=" dataCell  "><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;srKp=001">webapi</a></td>
    </tr>
    <!-- ListRow -->
    <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">
      <td class="actionColumn"><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;srKp=001&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">Edit</a></td>
      <th
        class=" dataCell  " scope="row"><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;srKp=001">Edwin,Nelson</a></th>
        <td
          class=" dataCell  ">&nbsp;</td>
          <td class=" dataCell  ">(353) 242-3423</td>
          <td class=" dataCell  "><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;srKp=001">webapi</a></td>
    </tr>
    <!-- ListRow -->
    <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">
      <td class="actionColumn"><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;srKp=001&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">Edit</a></td>
      <th
        class=" dataCell  " scope="row"><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;srKp=001">NELSON,EDWIN</a></th>
        <td
          class=" dataCell  ">&nbsp;</td>
          <td class=" dataCell  ">(656) 757-5463</td>
          <td class=" dataCell  "><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;srKp=001">webapi</a></td>
    </tr>
  </tbody>
</table>

Code Java:

          // 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<WebElement> 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();

Je n'ai aucune idée de l'endroit où je vais mal.
Toute aide serait appréciée.
Merci

0
user2560019 21 avril 2017 à 11:16

3 réponses

Meilleure réponse

Ici, après chaque clic, vous accédez à une autre page, les éléments deviendront donc obsolètes. Dans ce cas, vous devez à nouveau initialiser les éléments Web, sinon le pilote lèvera une exception de référence d'élément périmé.

Évitez également les chemins d'accès absolus lors de la recherche d'éléments.

Essayez le code comme ci-dessous:

    WebElement table = driver.findElement(By.xpath("//table"));
    List<WebElement> allRows = table.findElements(By.tagName("tr"));
    System.out.println(allRows.size());

    // And iterate over them, getting the cells

    for(int i=1;i<allRows.size();i++){

        WebElement table1 = driver.findElement(By.xpath("//table"));
        List<WebElement> allRows1 = table1.findElements(By.tagName("tr"));
        allRows1.get(i).findElement(By.xpath(".//td[normalize-space()='Edit']/following-sibling::th/a")).click();
        Thread.sleep(2000);
        driver.navigate().back();

    }

Faites-moi savoir si vous avez des questions

1
Akarsh 21 avril 2017 à 14:58

Lorsque nous essayons de trouver un élément par rapport à l'élément de ligne, j'espère que "//" n'est pas nécessaire. Si vous avez utilisé ceci, alors xpath est modifié, ce sera un nouvel élément mais pas un enfant de ligne. Essayez en changeant de chemin

row.findElement(By.xpath("td[normalize-space()='Edit']/following-sibling::th/a")).click();
0
murali selenium 21 avril 2017 à 08:43

Veuillez poster la source liée au HTML et le message d'erreur lancé, je pense que c'est parce que le xpath est incorrect.

0
adee alamsz 21 avril 2017 à 08:27