Python+selenium get all Bug titles in Zen Road

Source: Internet
Author: User

Objective:

For a lot of data, a page loading, need to page loading, such as the number of Bugs Zen Tao, a page default is 20 (you can change according to needs), then there is the second page, the third page and so on.

What do you do if you want to get all the bug titles?

Click on the next page bug, you will find the URL changes, only the last number changes, such as:

General idea:

Get all URL→DDT drivers to get data for each page

Steps:

First step: Get all URLs

This shows a total of several pages and the number of pages currently on the page, which we want to get on the back of the number ' 3 '.

Locate this element first, and then take the following ' 3 ' out of the regular code, as follows:

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 pages:', Total_ Page

The next step is to use the range function, get all the URL address, the code is as follows:

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

The final console output is as follows:

Under observation, is not only the back of the next-of-the--------------the other, no change, you can add these URLs to a list to use for the next DDT drive data

The specific code is as follows:

#Coding:utf-8 fromSelenium.webdriver.firefox.webdriverImportWebdriver as FirefoxImportRe,time fromSelenium.webdriver.support.uiImportwebdriverwaitclassGetUrl ():" "get all URL pages" "    defGet_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')#Enter URLWebdriverwait (self.driver,20,1). Until (LambdaX:X.FIND_ELEMENT_BY_ID (" Account"). Send_keys (username)#Enter your accountWebdriverwait (self.driver,20,1). Until (LambdaX:x.find_element_by_name ("Password"). Send_keys (PSW)#Enter PasswordWebdriverwait (self.driver,20,1). Until (LambdaX:X.FIND_ELEMENT_BY_ID ('Submit'). Click ()#Click the login buttonTime.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')#get to buglist this formTable_rows=table.find_elements_by_tag_name ('TR')#Get row Count        Print(('number of data per page:'), (Len (table_rows)-2))#minus 2 Here is the top and bottom of the table.B=self.driver.find_element_by_xpath (".//*[@id = ' buglist ']/tfoot/tr/td/div[2]/div/strong[2]")#Navigate to page show total pages that element (1/3)Page=re.findall (R'/(.+?)', B.text)#Remove the total number of pages from the back (that is, the 3)Total_page=Page[0]Print('Total number of pages:', Total_page) a=[]#Create an empty list to receive the generated URL         forIinchRange (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 ()returnaif __name__=='__main__': URL='http://127.0.0.1'username='Admin'PSW='123456'a=GetUrl () a.get_url (URL,USERNAME,PSW)

Step Two: Write another class to execute

1. Import the URL result of the first step

2. Adding DDT drivers

3. Write the results into txt

The specific code is as follows:

#Coding:utf-8ImportUnitTestImportDDTImport Time fromCom. Practice.test_geturlImportGETURL fromSeleniumImportWebdriver fromSelenium.webdriver.support.uiImportWebdriverwaiturl='http://127.0.0.1'username='Admin'PSW='123456'Test_url=GetUrl (). Get_url (URL,USERNAME,PSW) @ddt. DDTclassTest (unittest. TestCase):" "get all Bug titles" "@ddt. Data (*Test_url)deftest_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 (LambdaX:X.FIND_ELEMENT_BY_ID (" Account"). Send_keys ('Admin') webdriverwait (Self.driver,20,1). Until (LambdaX:x.find_element_by_name ("Password"). Send_keys ('123456') webdriverwait (Self.driver,20,1). Until (LambdaX: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")         forIinchA:Print(I.text)Try: With open ('Zendao.txt','a') as F:f.write (I.text+'\ n')              exceptException as msg:Print('Error writing:%s'%msg) Time.sleep (1)    defTearDown (self): Self.driver.close ()if __name__=='__main__': Unittest.main ()

The text information obtained here is obtained through table positioning.

Final txt print result (not truncated):

This is a coupon generated by the URL, but also pagination display, the principle and the above is basically the same.

Conclusion:

This is a bit of a hassle and there should be an easier way. Only pity the small white one, the ability is insufficient, temporarily can only first do so, welcome everybody to advise!!!

Python+selenium get all Bug titles in Zen Road

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.