简体   繁体   English

如何遍历 div selenium 的子元素

[英]How to iterate through child elements of a div selenium

I have been trying to scrap a java based website for the table.我一直在尝试为该表废弃一个基于 Java 的网站。 However the Table data are separated in all different tables.但是,表数据在所有不同的表中都是分开的。 I need to scrap the names and Roles off this table.我需要从这张桌子上删除名称和角色。

I unfortunately cannot give the URL out as its an internal website but I have attached the html code, Names have also been changed for security reasons不幸的是,我无法将 URL 作为其内部网站提供,但我附上了 html 代码,出于安全原因,名称也已更改

This is the website code:这是网站代码:

<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>

There is a possibility for future additions to users so I cannot have a set number for the loop.将来有可能增加用户,所以我不能为循环设置一个数字。

My code is the following:我的代码如下:

#Scrape the user table 
Username = driver.find_elements(By.XPATH, value="/html/body/div[7]/div/div[3]/div[1]/div/div/div[2]/div/div[3]/div/div/div/div/div[1]/table/tbody/tr/td[1]")
Role = driver.find_elements(By.XPATH, value="/html/body/div[7]/div/div[3]/div[1]/div/div/div[2]/div/div[3]/div/div/div/div/div[1]/table/tbody/tr/td[3]")  

for i in range(len(Username)):

    if Username[i].text in userdict:
        UserGet = userdict.get(Username[i].text)
        print("Company Name|F|DS0000|Company Role|" + UserGet + "|enabled|||" + Role[i].text)
        List.append("Company Name|F|DS0000|Company Role|" + UserGet + "|enabled|||" + Role[i].text)
            

    else: 
        print(Username[i].text + " Not in User Dictionary")

I hope my question makes sense and appreciate any help provided我希望我的问题有意义并感谢提供的任何帮助

Edit:编辑:

Name 1 = /html/body/div[7]/div/div[3]/div[1]/div/div/div[2]/div/div[3]/div/div/div/div/div[1]/table/tbody/tr/td[1]名称 1 = /html/body/div[7]/div/div[3]/div[1]/div/div/div[2]/div/div[3]/div/div/div/div/div [1]/表/tbody/tr/td[1]

Name 2 = /html/body/div[7]/div/div[3]/div[1]/div/div/div[2]/div/div[3]/div/div/div/div/div[2]/table/tbody/tr/td[1]名称 2 = /html/body/div[7]/div/div[3]/div[1]/div/div/div[2]/div/div[3]/div/div/div/div/div [2]/表/tbody/tr/td[1]

Somehow I need to loop through the divs but have and end point不知何故,我需要遍历 div 但有和终点

If you correct the XPath values it should give you the expected result.如果您更正 XPath 值,它应该会给您预期的结果。 I tested it using this website http://xpather.com/ but I'm not so great with dictionaries in Python so I could not properly test the code.我使用这个网站http://xpather.com/对其进行了测试,但我对 Python 中的字典不是很好,所以我无法正确测试代码。 Let me know if there are any issues and I can try to resolve them.让我知道是否有任何问题,我可以尝试解决它们。

#Scrape the user table 
Username = driver.find_elements(By.XPATH, value="//td[@role][1]")
Role = driver.find_elements(By.XPATH, value="//td/span[@tooltipkey]")  

for i in range(len(Username)):

    if Username[i].text in userdict:
        UserGet = userdict.get(Username[i].text)
        print("Company Name|F|DS0000|Company Role|" + UserGet + "|enabled|||" + Role[i].text)
        List.append("Company Name|F|DS0000|Company Role|" + UserGet + "|enabled|||" + Role[i].text)
            

    else: 
        print(Username[i].text + " Not in User Dictionary")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM