1. 程式人生 > >Mac下python+selenium【2】獲取表格所有元素

Mac下python+selenium【2】獲取表格所有元素

寫在最前面:

一點selenium自動化測試的常用技巧介紹,小白專用。

 

我郵的研究生網站上的一個table,想要把所有的資訊扒下來,怎麼處理呢?

<tr height="19">
    <td style="border-bottom:#000000 1px solid;text-align:center;border-left:#000000 1px solid;font-style:normal;width:110px;height:76px;color:#000000;font-size:13px;vertical-align:middle;border-top:#000000 1px solid;font-weight:400;border-right:#000000 1px solid;text-decoration:none;mso-protection:locked visible" class="et3" rowspan="5" width="110" colspan="1">0810z1</td>
    <td style="border-bottom:#000000 1px solid;text-align:center;border-left:#000000 1px solid;font-style:normal;width:162px;height:76px;color:#000000;font-size:13px;vertical-align:middle;border-top:#000000 1px solid;font-weight:400;border-right:#000000 1px solid;text-decoration:none;mso-protection:locked visible" class="et3" rowspan="5" width="162" colspan="1">資訊保安</td>
    <td style="border-bottom:#000000 1px solid;text-align:center;border-left:#000000 1px solid;font-style:normal;width:109px;height:19px;color:#000000;font-size:13px;vertical-align:middle;border-top:#000000 1px solid;font-weight:400;border-right:#000000 1px solid;text-decoration:none;mso-protection:locked visible" class="et3" height="19" width="109"><a href="http://yjs.njupt.edu.cn/epstar/web/outer/dsfc_ny_.jsp?dsgh=19980003">陳丹偉</a></td>
    <td style="border-bottom:#000000 1px solid;text-align:center;border-left:#000000 1px solid;font-style:normal;width:72px;height:19px;color:#000000;font-size:13px;vertical-align:middle;border-top:#000000 1px solid;font-weight:400;border-right:#000000 1px solid;text-decoration:none;mso-protection:locked visible" class="et3" height="19" width="72"><a href="http://yjs.njupt.edu.cn/epstar/web/outer/dsfc_ny_.jsp?dsgh=20120102">何利文</a></td>
    <td style="border-bottom:#000000 1px solid;text-align:center;border-left:#000000 1px solid;font-style:normal;width:72px;height:19px;color:#000000;font-size:13px;vertical-align:middle;border-top:#000000 1px solid;font-weight:400;border-right:#000000 1px solid;text-decoration:none;mso-protection:locked visible" class="et3" height="19" width="72"><a href="http://yjs.njupt.edu.cn/epstar/web/outer/dsfc_ny_.jsp?dsgh=20120081">蔣國平</a></td>
    <td style="border-bottom:#000000 1px solid;text-align:center;border-left:#000000 1px solid;font-style:normal;width:72px;height:19px;color:#000000;font-size:13px;vertical-align:middle;border-top:#000000 1px solid;font-weight:400;border-right:#000000 1px solid;text-decoration:none;mso-protection:locked visible" class="et3" height="19" width="72"><a href="http://yjs.njupt.edu.cn/epstar/web/outer/dsfc_ny_.jsp?dsgh=20070009">荊曉遠</a></td>
    <td style="border-bottom:#000000 1px solid;text-align:center;border-left:#000000 1px solid;font-style:normal;width:72px;height:19px;color:#000000;font-size:13px;vertical-align:middle;border-top:#000000 1px solid;font-weight:400;border-right:#000000 1px solid;text-decoration:none;mso-protection:locked visible" class="et3" height="19" width="72"><a href="http://yjs.njupt.edu.cn/epstar/web/outer/dsfc_ny_.jsp?dsgh=20020022">孫國梓</a></td></tr>

 這是一個html頁面,很顯然只有一個tr-代表一行,td-代表一列,那麼我們不需要遍歷所有行,只需找到一行的所有列即可。

由於該table沒有id之類的,我們用xpath找到table所在位置,然後找到該行所有列,遍歷即可。

 

首先copy該table的xpath

 

然後通過tag_name = 'td'遍歷所有列

from selenium import webdriver
import time

if __name__ == "__main__":
    driver = webdriver.Chrome()
    driver.get('http://cs.njupt.edu.cn/2010/0510/c9392a110578/page.htm')
    trs = driver.find_element_by_xpath('/html/body/div[4]/div/div[2]/div/div/div/div/table[1]')
    tds = trs.find_elements_by_tag_name('td')
    print(trs)
    print(tds)
    time.sleep(3)
    info = []
    for td in tds:
        info.append(td.text)
    print(info)

 

最後列印結果:

['0810z1', '資訊保安', '陳丹偉', '何利文', '蔣國平', '荊曉遠', '孫國梓', 
'吳\u3000蒙', '楊 \u3000庚', '張  偉', '張迎周', '曹曉梅', 
'陳  偉', '魯蔚鋒', '任勳益', '張  潔', '王少輝', 
'王志偉', '張  琳', ' 陳國良', ' 許\u3000建', ' 王化群', 
'宋玉蓉', '操曉春*', '', '', '']

 

如果有多行的話,再遍歷一次行即可:

for tr in trs:
    for td in tr.find_elements_by_tag_name('td'):
        info.append(td.text)
print(info)