場景
我們在測試一個web 應用時,經常出現翻頁的情況,下面介紹翻頁場景
代碼
#!/usr/bin/env python # -*- codinfg:utf-8 -*- ''' @author: Jeff LEE @file: 翻頁.py @time: 2018-09-26 11:14 @desc: ''' from selenium import webdriver from selenium.webdriver.common.action_chains import ActionChains import time driver = webdriver.Firefox() #添加智能等待 driver.implicitly_wait(10) driver.get('https://www.baidu.com/') driver.find_element_by_id('kw').send_keys('uniquefu') driver.find_element_by_id('su').click() page = driver.find_element_by_id('page') pages = page.find_elements_by_tag_name('a') #查找所有翻頁跳轉鏈接 time.sleep(5) js = 'document.documentElement.scrollTop=10000' total = 0 #頁面數 is_next_page = True #存在下一頁 page_num = 0 #要點擊的頁面號 #往后翻頁 while page_num <10: #也可以通過is_next_page進行判斷循環 driver.execute_script(js) page_num = page_num + 1 #設置頁號為下一頁 total = page_num #記錄頁面數 value=str(page_num) try: #查找指定頁面 xpath= "//div[@id='page']/a[contains(@href,'pn=%s')]" %value print(xpath) one_page = driver.find_element_by_xpath(xpath) 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 #往前翻頁 while total >= 0: driver.execute_script(js) try: total = total -1 value = str(total) xpath = "//div[@id='page']/a[contains(@href,'pn=%s')]" % value print(xpath) one_page = driver.find_element_by_xpath(xpath) one_page.click() time.sleep(1) driver.execute_script(js) time.sleep(1) except: print('no pre page') break; time.sleep(3) driver.quit()
遇到問題:
selenium.common.exceptions.StaleElementReferenceException: Message: u'Element not found in the cache - perhaps the page has changed since it was looked up' ; Stacktrace:
即在cache中找不到元素,可能是在元素被找到之后頁面變換了。 這就說明,當前頁面發生跳轉之后,存在cache中的與這個頁面相關的元素也被清空了,因此跳轉后需要重新獲取下一個頁面翻頁鏈接,然后點擊。
備注:
對於類型博客類型的翻頁不需要那么麻煩,因為翻頁后頁面鏈接不會發生變化