使用Selenium从基于JavaScript的表中取消名称

发布于 2025-02-13 11:09:18 字数 15514 浏览 2 评论 0原文

大家好,我正在尝试从基于JavaScript的网站上取出一张桌子。但是,这很奇怪,因为表格分为不同的表标签。我无法在内部服务器上共享网站,但已在下面附加了一些HTML代码:

<div role="presentation" style="position: absolute; left: 0px; top: 0px;">
   <div class="dojoxGridRow" role="row" aria-selected="false" idref="admin" style="">
      <table class="dojoxGridRowTable" border="0" cellspacing="0" cellpadding="0" role="presentation">
         <tbody>
            <tr>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell" idx="0" style="width:14em;" hilite="1" fieldname="name">admin</td>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell" idx="1" style="width:12em;" hilite="1" fieldname="state">
                  <div class="stateCouple userState" data-state="locked">
                     <div class="sprite warning13 mar5r"></div>
                     Locked
                  </div>
               </td>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell" idx="2" style="width:14em;" hilite="1" fieldname="role"><span class="hasMapTooltip" tooltipkey="admin">Administrator</span></td>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="3" style="display:none;width:10em;" hilite="1" fieldname="scope">*</td>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="4" style="display:none;width:16em;" hilite="1" fieldname="lastAuthenticatedTime">08/07/13 07:17:49 PM</td>
            </tr>
         </tbody>
      </table>
   </div>
   <div class="dojoxGridRow dojoxGridRowOdd" role="row" aria-selected="false" idref="user1" style="">
      <table class="dojoxGridRowTable" border="0" cellspacing="0" cellpadding="0" role="presentation">
         <tbody>
            <tr>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="0" style="width:14em;" hilite="1" fieldname="name">user1</td>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="1" style="width:12em;" hilite="1" fieldname="state">
                  <div class="stateCouple userState" data-state="connected">
                     <div class="sprite checkMark13 mar5r"></div>
                     Connected (2)
                  </div>
               </td>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="2" style="width:14em;" hilite="1" fieldname="role"><span class="hasMapTooltip" tooltipkey="admin">Administrator</span></td>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="3" style="display:none;width:10em;" hilite="1" fieldname="scope">*</td>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="4" style="display:none;width:16em;" hilite="1" fieldname="lastAuthenticatedTime">07/04/22 03:37:32 PM</td>
            </tr>
         </tbody>
      </table>
   </div>
   <div class="dojoxGridRow" role="row" aria-selected="false" idref="user2" style="">
      <table class="dojoxGridRowTable" border="0" cellspacing="0" cellpadding="0" role="presentation">
         <tbody>
            <tr>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="0" style="width:14em;" hilite="1" fieldname="name">user2</td>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="1" style="width:12em;" hilite="1" fieldname="state">
                  <div class="stateCouple userState" data-state="disconnected">
                     <div class="sprite disabledMark13 mar5r"></div>
                     Disconnected
                  </div>
               </td>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="2" style="width:14em;" hilite="1" fieldname="role"><span class="hasMapTooltip" tooltipkey="admin">Administrator</span></td>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="3" style="display:none;width:10em;" hilite="1" fieldname="scope">*</td>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="4" style="display:none;width:16em;" hilite="1" fieldname="lastAuthenticatedTime">06/27/22 09:55:30 AM</td>
            </tr>
         </tbody>
      </table>
   </div>
   <div class="dojoxGridRow dojoxGridRowOdd" role="row" aria-selected="false" idref="user3" style="">
      <table class="dojoxGridRowTable" border="0" cellspacing="0" cellpadding="0" role="presentation">
         <tbody>
            <tr>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="0" style="width:14em;" hilite="1" fieldname="name">user3</td>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="1" style="width:12em;" hilite="1" fieldname="state">
                  <div class="stateCouple userState" data-state="disconnected">
                     <div class="sprite disabledMark13 mar5r"></div>
                     Disconnected
                  </div>
               </td>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="2" style="width:14em;" hilite="1" fieldname="role"><span class="hasMapTooltip" tooltipkey="admin">Administrator</span></td>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="3" style="display:none;width:10em;" hilite="1" fieldname="scope">*</td>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="4" style="display:none;width:16em;" hilite="1" fieldname="lastAuthenticatedTime">12/18/19 03:56:05 PM</td>
            </tr>
         </tbody>
      </table>
   </div>
   <div class="dojoxGridRow" role="row" aria-selected="false" idref="user4" style="">
      <table class="dojoxGridRowTable" border="0" cellspacing="0" cellpadding="0" role="presentation">
         <tbody>
            <tr>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="0" style="width:14em;" hilite="1" fieldname="name">user4</td>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="1" style="width:12em;" hilite="1" fieldname="state">
                  <div class="stateCouple userState" data-state="disconnected">
                     <div class="sprite disabledMark13 mar5r"></div>
                     Disconnected
                  </div>
               </td>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="2" style="width:14em;" hilite="1" fieldname="role"><span class="hasMapTooltip" tooltipkey="admin">Administrator</span></td>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="3" style="display:none;width:10em;" hilite="1" fieldname="scope">*</td>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="4" style="display:none;width:16em;" hilite="1" fieldname="lastAuthenticatedTime">05/20/22 05:49:45 PM</td>
            </tr>
         </tbody>
      </table>
   </div>
   <div class="dojoxGridRow dojoxGridRowOdd" role="row" aria-selected="false" idref="user5" style="">
      <table class="dojoxGridRowTable" border="0" cellspacing="0" cellpadding="0" role="presentation">
         <tbody>
            <tr>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="0" style="width:14em;" hilite="1" fieldname="name">user5</td>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="1" style="width:12em;" hilite="1" fieldname="state">
                  <div class="stateCouple userState" data-state="disconnected">
                     <div class="sprite disabledMark13 mar5r"></div>
                     Disconnected
                  </div>
               </td>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="2" style="width:14em;" hilite="1" fieldname="role"><span class="hasMapTooltip" tooltipkey="admin">Administrator</span></td>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="3" style="display:none;width:10em;" hilite="1" fieldname="scope">*</td>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="4" style="display:none;width:16em;" hilite="1" fieldname="lastAuthenticatedTime">05/19/22 12:16:31 PM</td>
            </tr>
         </tbody>
      </table>
   </div>
   <div class="dojoxGridRow" role="row" aria-selected="false" idref="user6" style="">
      <table class="dojoxGridRowTable" border="0" cellspacing="0" cellpadding="0" role="presentation">
         <tbody>
            <tr>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="0" style="width:14em;" hilite="1" fieldname="name">user6</td>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="1" style="width:12em;" hilite="1" fieldname="state">
                  <div class="stateCouple userState" data-state="disconnected">
                     <div class="sprite disabledMark13 mar5r"></div>
                     Disconnected
                  </div>
               </td>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="2" style="width:14em;" hilite="1" fieldname="role"><span class="hasMapTooltip" tooltipkey="admin">Administrator</span></td>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="3" style="display:none;width:10em;" hilite="1" fieldname="scope">*</td>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="4" style="display:none;width:16em;" hilite="1" fieldname="lastAuthenticatedTime">07/01/22 03:24:16 PM</td>
            </tr>
         </tbody>
      </table>
   </div>
   <div class="dojoxGridRow dojoxGridRowOdd" role="row" aria-selected="false" idref="secadmin" style="">
      <table class="dojoxGridRowTable" border="0" cellspacing="0" cellpadding="0" role="presentation">
         <tbody>
            <tr>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="0" style="width:14em;" hilite="1" fieldname="name">secadmin</td>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="1" style="width:12em;" hilite="1" fieldname="state">
                  <div class="stateCouple userState" data-state="locked">
                     <div class="sprite warning13 mar5r"></div>
                     Locked
                  </div>
               </td>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="2" style="width:14em;" hilite="1" fieldname="role"><span class="hasMapTooltip" tooltipkey="security_admin">Security administrator</span></td>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="3" style="display:none;width:10em;" hilite="1" fieldname="scope">*</td>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="4" style="display:none;width:16em;" hilite="1" fieldname="lastAuthenticatedTime">06/07/21 03:28:40 PM</td>
            </tr>
         </tbody>
      </table>
   </div>
   <div class="dojoxGridRow" role="row" aria-selected="false" idref="tpcuser" style="">
      <table class="dojoxGridRowTable" border="0" cellspacing="0" cellpadding="0" role="presentation">
         <tbody>
            <tr>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="0" style="width:14em;" hilite="1" fieldname="name">tpcuser</td>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="1" style="width:12em;" hilite="1" fieldname="state">
                  <div class="stateCouple userState" data-state="disconnected">
                     <div class="sprite disabledMark13 mar5r"></div>
                     Disconnected
                  </div>
               </td>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="2" style="width:14em;" hilite="1" fieldname="role"><span class="hasMapTooltip" tooltipkey="monitor">Monitor</span></td>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="3" style="display:none;width:10em;" hilite="1" fieldname="scope">PUBLIC</td>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="4" style="display:none;width:16em;" hilite="1" fieldname="lastAuthenticatedTime">03/03/21 06:00:33 PM</td>
            </tr>
         </tbody>
      </table>
   </div>
   <div class="dojoxGridRow dojoxGridRowOdd" role="row" aria-selected="false" idref="user6" style="">
      <table class="dojoxGridRowTable" border="0" cellspacing="0" cellpadding="0" role="presentation">
         <tbody>
            <tr>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="0" style="width:14em;" hilite="1" fieldname="name">user6</td>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="1" style="width:12em;" hilite="1" fieldname="state">
                  <div class="stateCouple userState" data-state="disconnected">
                     <div class="sprite disabledMark13 mar5r"></div>
                     Disconnected
                  </div>
               </td>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="2" style="width:14em;" hilite="1" fieldname="role"><span class="hasMapTooltip" tooltipkey="admin">Administrator</span></td>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="3" style="display:none;width:10em;" hilite="1" fieldname="scope">*</td>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="4" style="display:none;width:16em;" hilite="1" fieldname="lastAuthenticatedTime">05/10/22 12:39:54 PM</td>
            </tr>
         </tbody>
      </table>
   </div>
</div>

我的代码:

for i in range(10):
    print(i)
    Username = driver.find_elements(By.XPATH, value="//*[@id='dojox_grid_View_1']/div/div/div/div[" + str(i) + "]/table/tbody/tr/td[1]")
    print(Username)

我对如何正确通过表的XPath如下感到困惑:

admin = //*[@id='dojox_grid_View_1']/div/div/div/div[1]/table/tbody/tr/td[1]
User1 = //*[@id='dojox_grid_View_1']/div/div/div/div[2]/table/tbody/tr/td[1]
User2 = //*[@id='dojox_grid_View_1']/div/div/div/div[3]/table/tbody/tr/td[1]
etc. 

我希望输出在列表中,例如

[admin, user1, user2, user3, user4,] 

I很抱歉,如果这个问题令人困惑,我会尽力而为

Hello Everyone I am trying to scrap a table from a Javascript based website. It is however quite strange as the table is split up into different table tags. I cannot share the website as its on an internal server but have attached some html code below:

<div role="presentation" style="position: absolute; left: 0px; top: 0px;">
   <div class="dojoxGridRow" role="row" aria-selected="false" idref="admin" style="">
      <table class="dojoxGridRowTable" border="0" cellspacing="0" cellpadding="0" role="presentation">
         <tbody>
            <tr>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell" idx="0" style="width:14em;" hilite="1" fieldname="name">admin</td>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell" idx="1" style="width:12em;" hilite="1" fieldname="state">
                  <div class="stateCouple userState" data-state="locked">
                     <div class="sprite warning13 mar5r"></div>
                     Locked
                  </div>
               </td>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell" idx="2" style="width:14em;" hilite="1" fieldname="role"><span class="hasMapTooltip" tooltipkey="admin">Administrator</span></td>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="3" style="display:none;width:10em;" hilite="1" fieldname="scope">*</td>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="4" style="display:none;width:16em;" hilite="1" fieldname="lastAuthenticatedTime">08/07/13 07:17:49 PM</td>
            </tr>
         </tbody>
      </table>
   </div>
   <div class="dojoxGridRow dojoxGridRowOdd" role="row" aria-selected="false" idref="user1" style="">
      <table class="dojoxGridRowTable" border="0" cellspacing="0" cellpadding="0" role="presentation">
         <tbody>
            <tr>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="0" style="width:14em;" hilite="1" fieldname="name">user1</td>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="1" style="width:12em;" hilite="1" fieldname="state">
                  <div class="stateCouple userState" data-state="connected">
                     <div class="sprite checkMark13 mar5r"></div>
                     Connected (2)
                  </div>
               </td>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="2" style="width:14em;" hilite="1" fieldname="role"><span class="hasMapTooltip" tooltipkey="admin">Administrator</span></td>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="3" style="display:none;width:10em;" hilite="1" fieldname="scope">*</td>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="4" style="display:none;width:16em;" hilite="1" fieldname="lastAuthenticatedTime">07/04/22 03:37:32 PM</td>
            </tr>
         </tbody>
      </table>
   </div>
   <div class="dojoxGridRow" role="row" aria-selected="false" idref="user2" style="">
      <table class="dojoxGridRowTable" border="0" cellspacing="0" cellpadding="0" role="presentation">
         <tbody>
            <tr>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="0" style="width:14em;" hilite="1" fieldname="name">user2</td>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="1" style="width:12em;" hilite="1" fieldname="state">
                  <div class="stateCouple userState" data-state="disconnected">
                     <div class="sprite disabledMark13 mar5r"></div>
                     Disconnected
                  </div>
               </td>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="2" style="width:14em;" hilite="1" fieldname="role"><span class="hasMapTooltip" tooltipkey="admin">Administrator</span></td>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="3" style="display:none;width:10em;" hilite="1" fieldname="scope">*</td>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="4" style="display:none;width:16em;" hilite="1" fieldname="lastAuthenticatedTime">06/27/22 09:55:30 AM</td>
            </tr>
         </tbody>
      </table>
   </div>
   <div class="dojoxGridRow dojoxGridRowOdd" role="row" aria-selected="false" idref="user3" style="">
      <table class="dojoxGridRowTable" border="0" cellspacing="0" cellpadding="0" role="presentation">
         <tbody>
            <tr>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="0" style="width:14em;" hilite="1" fieldname="name">user3</td>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="1" style="width:12em;" hilite="1" fieldname="state">
                  <div class="stateCouple userState" data-state="disconnected">
                     <div class="sprite disabledMark13 mar5r"></div>
                     Disconnected
                  </div>
               </td>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="2" style="width:14em;" hilite="1" fieldname="role"><span class="hasMapTooltip" tooltipkey="admin">Administrator</span></td>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="3" style="display:none;width:10em;" hilite="1" fieldname="scope">*</td>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="4" style="display:none;width:16em;" hilite="1" fieldname="lastAuthenticatedTime">12/18/19 03:56:05 PM</td>
            </tr>
         </tbody>
      </table>
   </div>
   <div class="dojoxGridRow" role="row" aria-selected="false" idref="user4" style="">
      <table class="dojoxGridRowTable" border="0" cellspacing="0" cellpadding="0" role="presentation">
         <tbody>
            <tr>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="0" style="width:14em;" hilite="1" fieldname="name">user4</td>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="1" style="width:12em;" hilite="1" fieldname="state">
                  <div class="stateCouple userState" data-state="disconnected">
                     <div class="sprite disabledMark13 mar5r"></div>
                     Disconnected
                  </div>
               </td>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="2" style="width:14em;" hilite="1" fieldname="role"><span class="hasMapTooltip" tooltipkey="admin">Administrator</span></td>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="3" style="display:none;width:10em;" hilite="1" fieldname="scope">*</td>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="4" style="display:none;width:16em;" hilite="1" fieldname="lastAuthenticatedTime">05/20/22 05:49:45 PM</td>
            </tr>
         </tbody>
      </table>
   </div>
   <div class="dojoxGridRow dojoxGridRowOdd" role="row" aria-selected="false" idref="user5" style="">
      <table class="dojoxGridRowTable" border="0" cellspacing="0" cellpadding="0" role="presentation">
         <tbody>
            <tr>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="0" style="width:14em;" hilite="1" fieldname="name">user5</td>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="1" style="width:12em;" hilite="1" fieldname="state">
                  <div class="stateCouple userState" data-state="disconnected">
                     <div class="sprite disabledMark13 mar5r"></div>
                     Disconnected
                  </div>
               </td>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="2" style="width:14em;" hilite="1" fieldname="role"><span class="hasMapTooltip" tooltipkey="admin">Administrator</span></td>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="3" style="display:none;width:10em;" hilite="1" fieldname="scope">*</td>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="4" style="display:none;width:16em;" hilite="1" fieldname="lastAuthenticatedTime">05/19/22 12:16:31 PM</td>
            </tr>
         </tbody>
      </table>
   </div>
   <div class="dojoxGridRow" role="row" aria-selected="false" idref="user6" style="">
      <table class="dojoxGridRowTable" border="0" cellspacing="0" cellpadding="0" role="presentation">
         <tbody>
            <tr>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="0" style="width:14em;" hilite="1" fieldname="name">user6</td>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="1" style="width:12em;" hilite="1" fieldname="state">
                  <div class="stateCouple userState" data-state="disconnected">
                     <div class="sprite disabledMark13 mar5r"></div>
                     Disconnected
                  </div>
               </td>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="2" style="width:14em;" hilite="1" fieldname="role"><span class="hasMapTooltip" tooltipkey="admin">Administrator</span></td>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="3" style="display:none;width:10em;" hilite="1" fieldname="scope">*</td>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="4" style="display:none;width:16em;" hilite="1" fieldname="lastAuthenticatedTime">07/01/22 03:24:16 PM</td>
            </tr>
         </tbody>
      </table>
   </div>
   <div class="dojoxGridRow dojoxGridRowOdd" role="row" aria-selected="false" idref="secadmin" style="">
      <table class="dojoxGridRowTable" border="0" cellspacing="0" cellpadding="0" role="presentation">
         <tbody>
            <tr>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="0" style="width:14em;" hilite="1" fieldname="name">secadmin</td>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="1" style="width:12em;" hilite="1" fieldname="state">
                  <div class="stateCouple userState" data-state="locked">
                     <div class="sprite warning13 mar5r"></div>
                     Locked
                  </div>
               </td>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="2" style="width:14em;" hilite="1" fieldname="role"><span class="hasMapTooltip" tooltipkey="security_admin">Security administrator</span></td>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="3" style="display:none;width:10em;" hilite="1" fieldname="scope">*</td>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="4" style="display:none;width:16em;" hilite="1" fieldname="lastAuthenticatedTime">06/07/21 03:28:40 PM</td>
            </tr>
         </tbody>
      </table>
   </div>
   <div class="dojoxGridRow" role="row" aria-selected="false" idref="tpcuser" style="">
      <table class="dojoxGridRowTable" border="0" cellspacing="0" cellpadding="0" role="presentation">
         <tbody>
            <tr>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="0" style="width:14em;" hilite="1" fieldname="name">tpcuser</td>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="1" style="width:12em;" hilite="1" fieldname="state">
                  <div class="stateCouple userState" data-state="disconnected">
                     <div class="sprite disabledMark13 mar5r"></div>
                     Disconnected
                  </div>
               </td>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="2" style="width:14em;" hilite="1" fieldname="role"><span class="hasMapTooltip" tooltipkey="monitor">Monitor</span></td>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="3" style="display:none;width:10em;" hilite="1" fieldname="scope">PUBLIC</td>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="4" style="display:none;width:16em;" hilite="1" fieldname="lastAuthenticatedTime">03/03/21 06:00:33 PM</td>
            </tr>
         </tbody>
      </table>
   </div>
   <div class="dojoxGridRow dojoxGridRowOdd" role="row" aria-selected="false" idref="user6" style="">
      <table class="dojoxGridRowTable" border="0" cellspacing="0" cellpadding="0" role="presentation">
         <tbody>
            <tr>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="0" style="width:14em;" hilite="1" fieldname="name">user6</td>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="1" style="width:12em;" hilite="1" fieldname="state">
                  <div class="stateCouple userState" data-state="disconnected">
                     <div class="sprite disabledMark13 mar5r"></div>
                     Disconnected
                  </div>
               </td>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="2" style="width:14em;" hilite="1" fieldname="role"><span class="hasMapTooltip" tooltipkey="admin">Administrator</span></td>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="3" style="display:none;width:10em;" hilite="1" fieldname="scope">*</td>
               <td tabindex="-1" role="gridcell" class="dojoxGridCell " idx="4" style="display:none;width:16em;" hilite="1" fieldname="lastAuthenticatedTime">05/10/22 12:39:54 PM</td>
            </tr>
         </tbody>
      </table>
   </div>
</div>

My Code:

for i in range(10):
    print(i)
    Username = driver.find_elements(By.XPATH, value="//*[@id='dojox_grid_View_1']/div/div/div/div[" + str(i) + "]/table/tbody/tr/td[1]")
    print(Username)

I am confused on how to loop through correctly as the table's XPATH are as follows:

admin = //*[@id='dojox_grid_View_1']/div/div/div/div[1]/table/tbody/tr/td[1]
User1 = //*[@id='dojox_grid_View_1']/div/div/div/div[2]/table/tbody/tr/td[1]
User2 = //*[@id='dojox_grid_View_1']/div/div/div/div[3]/table/tbody/tr/td[1]
etc. 

I want the output to be in a list e.g.

[admin, user1, user2, user3, user4,] 

I am so sorry if this question is confusing, I wrote it as best as I could

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝海 2025-02-20 11:09:18
  1. 查找所有表。
tables = driver.find_elements(By.XPATH, '//table[@class="dojoxGridRowTable"]')
  1. 循环这些表查找第一个TD。

    表[1:]将跳过第一个表,这是&lt; th&gt;的列名称包装)

print([table.find_element(By.XPATH, './tbody/tr/td[1]').text for table in tables[1:]])

/ -------测试-------------------------------------------- --- /

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager

driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))

driver.get('file:///D:/Documents/python/projects/test/test2.html')
tables = driver.find_elements(By.XPATH, '//table[@class="dojoxGridRowTable"]')

res = []
for table in tables:
    try:
        res.append(table.find_element(By.XPATH, './tbody/tr/td[1]').text)
    except:
        pass
print(res)

输出

['admin', 'user1', 'user2', 'user3', 'user4', 'user5', 'user6', 'secadmin', 'tpcuser', 'user6']
  1. Find all tables.
tables = driver.find_elements(By.XPATH, '//table[@class="dojoxGridRowTable"]')
  1. For loop these tables find first td.

    (tables[1:] will skip first table which are column names wrap by <th>)

print([table.find_element(By.XPATH, './tbody/tr/td[1]').text for table in tables[1:]])

/------test-------/

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager

driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))

driver.get('file:///D:/Documents/python/projects/test/test2.html')
tables = driver.find_elements(By.XPATH, '//table[@class="dojoxGridRowTable"]')

res = []
for table in tables:
    try:
        res.append(table.find_element(By.XPATH, './tbody/tr/td[1]').text)
    except:
        pass
print(res)

output

['admin', 'user1', 'user2', 'user3', 'user4', 'user5', 'user6', 'secadmin', 'tpcuser', 'user6']
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文