您的位置:軟件測(cè)試 > 開(kāi)源軟件測(cè)試 > 開(kāi)源功能測(cè)試工具 > Selenium
Selenium之百度搜索,結(jié)果列表翻頁(yè)查詢
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時(shí)間:[ 2015/1/16 14:51:37 ] 推薦標(biāo)簽:Selenium 功能測(cè)試工具 單元測(cè)試

#備注以下小段代碼描述了頁(yè)面變化規(guī)律,這個(gè)得自己去研究
if not has_pre_page:   #點(diǎn)擊第2頁(yè)時(shí)會(huì)出現(xiàn)上一頁(yè),頁(yè)號(hào)加1
has_pre_page = True
page_num = page_num + 1
if page_num % 7 == 0: #page_num等于7時(shí),頁(yè)號(hào)減1
page_num = page_num - 1
time.sleep(2)
driver.execute_script(js)
time.sleep(2)
time.sleep(3)
driver.quit()
  改進(jìn)版(自動(dòng)翻頁(yè),前翻頁(yè)后翻頁(yè))
# -*- coding: utf-8 -*-
from selenium import webdriver
import time
if __name__ == "__main__":
driver = webdriver.Firefox()
driver.maximize_window()
driver.get('http://www.baidu.com')
driver.implicitly_wait(5)
driver.find_element_by_id('kw1').send_keys('selenium selenium')#測(cè)試數(shù)據(jù) selenium zhidashso dld#selenium zhidashso dldld
driver.find_element_by_id('su1').click()
js = 'document.documentElement.scrollTop=10000'
total = 0  #頁(yè)面數(shù)
is_next_page = True  #存在下一頁(yè)
page_num = 0   #要點(diǎn)擊的頁(yè)面號(hào)
#往后翻頁(yè)
while is_next_page:#'sv_page=1' in one_page.get_attribute('href')
driver.execute_script(js)
page_num = page_num + 1    #設(shè)置頁(yè)號(hào)為下一頁(yè)
total = page_num   #記錄頁(yè)面數(shù)
value=str(page_num)
try:
#查找指定頁(yè)面
one_page = driver.find_element_by_css_selector('p[id="page"]>a[href*=pn='+value+']')
one_page.click()
time.sleep(1)
driver.execute_script(js)
time.sleep(1)
except:
print('no next page')
is_next_page = False
total = total - 1
break
#往前翻頁(yè)
while total >= 0:
driver.execute_script(js)
try:
total = total -1
value = str(total)
one_page = driver.find_element_by_css_selector('p[id="page"]>a[href*=pn='+value+']')
one_page.click()
time.sleep(1)
driver.execute_script(js)
time.sleep(1)
except:
print('no pre page')
break;
time.sleep(3)
driver.quit()

上一頁(yè)12下一頁(yè)
軟件測(cè)試工具 | 聯(lián)系我們 | 投訴建議 | 誠(chéng)聘英才 | 申請(qǐng)使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd