python+selenium擷取禪道所有Bug標題

來源:互聯網
上載者:User

標籤:element   sub   自己   \n   ota   return   優惠券   open   TE   

前言:

對於一組很多的資料,一個頁面載入不完,需要分頁載入,比如禪道的Bug數,一頁預設是20個(自己可以根據需求更改),這時就有了第二頁,第三頁等等。

這時如果要擷取所有的Bug標題來怎麼做呢?

點擊下一頁Bug,你會發現url的變化,就只有最後一個數字改變,如:

大體思路:

擷取所有url→ddt驅動擷取每一頁的資料

步驟:

第一步:擷取所有url

這裡已經顯示了總共有幾頁和當前所在的頁面數,我們要擷取的就後面的數字 ‘3’。

先定位到這個元素,在通過正則取出後面的 ‘3’,具體代碼如下:

b=self.driver.find_element_by_xpath(".//*[@id=‘bugList‘]/tfoot/tr/td/div[2]/div/strong[2]")page=re.findall(r‘/(.+?)‘,b.text)total_page=page[0]print(‘總共的頁數:‘,total_page)

接下來就是要用到range函數了,擷取所有的url地址了,代碼如下:

for i in range(1,int(total_page)+1):  Url=url+‘/zentao/bug-browse-1--unclosed-0--60-20-%s.html‘ % i  print(Url)

最後控制台輸出如下:

觀察下,是不是只有後面的1,2,3在跟著變,其他無任何變化,這時就可以將這些url添加到一個list中去,用做接下來的ddt驅動的資料了

具體代碼如下:

#coding:utf-8from selenium.webdriver.firefox.webdriver import WebDriver as Firefoximport re,timefrom selenium.webdriver.support.ui import WebDriverWaitclass GetUrl():    ‘‘‘擷取所有URL頁面‘‘‘    def get_url(self,url,username,psw):        self.driver=Firefox()        self.driver.maximize_window()        self.driver.set_page_load_timeout(20)        self.driver.implicitly_wait(20)        self.driver.get(url+‘/zentao/user-login-L3plbnRhby8=.html‘) #輸入網址        WebDriverWait(self.driver,20,1).until(lambda x:x.find_element_by_id("account")).send_keys(username) #輸入帳號        WebDriverWait(self.driver,20,1).until(lambda x:x.find_element_by_name("password")).send_keys(psw)   #輸入密碼        WebDriverWait(self.driver,20,1).until(lambda x:x.find_element_by_id(‘submit‘)).click()              #點擊登入按鈕        time.sleep(2)        self.driver.find_element_by_xpath(".//*[@id=‘mainmenu‘]/ul/li[4]/a").click()        time.sleep(2)        self.driver.find_element_by_xpath(".//*[@id=‘modulemenu‘]/ul/li[2]/a").click()        time.sleep(2)        table=self.driver.find_element_by_id(‘bugList‘)  #擷取到bugList這個表格        table_rows=table.find_elements_by_tag_name(‘tr‘) #擷取行數        print((‘每一頁資料的條數:‘),(len(table_rows)-2))    #這裡減2是減去表格最上面和最下面那行        b=self.driver.find_element_by_xpath(".//*[@id=‘bugList‘]/tfoot/tr/td/div[2]/div/strong[2]") #定位到頁面顯示總頁數那個元素(1/3)        page=re.findall(r‘/(.+?)‘,b.text)  #通過正則取出後面那個總頁數(也就是那個3)        total_page=page[0]        print(‘總共的頁數:‘,total_page)        a=[]  #建立空list去接收產生的url        for i in range(1,int(total_page)+1):            Url=url+‘/zentao/bug-browse-1--unclosed-0--60-20-%s.html‘ % i            a.append(Url)          print(a)        self.driver.close()        return aif __name__==‘__main__‘:    url=‘http://127.0.0.1‘    username=‘admin‘    psw=‘123456‘    a=GetUrl()    a.get_url(url,username,psw)

 

 

第二步:編寫另一個類來執行

1.匯入第一步的url結果

 2.添加ddt驅動

3.將結果寫入TXT中

具體代碼如下:

#coding:utf-8import unittestimport ddtimport timefrom com.Practice.test_geturl import GetUrlfrom selenium import webdriverfrom selenium.webdriver.support.ui import WebDriverWaiturl=‘http://127.0.0.1‘username=‘admin‘psw=‘123456‘test_url=GetUrl().get_url(url,username,psw)@ddt.ddtclass Test(unittest.TestCase):    ‘‘‘擷取所有Bug標題‘‘‘    @ddt.data(*test_url)    def test_01(self,test_url):        self.driver = webdriver.Firefox()        self.driver.get(‘http://127.0.0.1/zentao/user-login-L3plbnRhby8=.html‘)        WebDriverWait(self.driver,20,1).until(lambda x:x.find_element_by_id("account")).send_keys(‘admin‘)        WebDriverWait(self.driver,20,1).until(lambda x:x.find_element_by_name("password")).send_keys(‘123456‘)        WebDriverWait(self.driver,20,1).until(lambda x:x.find_element_by_id(‘submit‘)).click()        time.sleep(1)        self.driver.get(test_url)        a=self.driver.find_elements_by_xpath(".//*[@id=‘bugList‘]/tbody/tr/td[4]/a")        for i in a:            print(i.text)            try:                with open(‘zendao.txt‘,‘a‘) as f:                    f.write(i.text+‘\n‘)              except Exception as msg:                print(‘寫入時出錯啦:%s‘ % msg)             time.sleep(1)    def tearDown(self):        self.driver.close()if __name__==‘__main__‘:    unittest.main()

這裡擷取文本資訊是通過table定位擷取。

最後TXT列印結果(沒截全):

 

這個是一個優惠券產生的網址,也是分頁顯示,原理和上面大體相同。

結語:

 這個有點麻煩,應該還有更簡單的方法。只可惜小白一枚,能力不足,暫時就只能先這樣做了,歡迎大家提意見!!!

 

python+selenium擷取禪道所有Bug標題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.