接触的项目分页的形式是以下形式:

selenium + python自动化测试unittest框架学习(六)分页

想要获取总页数后,遍历执行翻页的功能,但由于分页是以javascript方法实现的,每次点击确定按钮后,页面就回刷新,webelement元素过期无法遍历下一个进行翻页操作,报StaleElementReferenceException的错误,所以对于这个操作折腾了半天还是放弃了。

对于分页的操作实现了以下功能:

(1)获取总页数并输出

(2)遍历操作‘上一页’,‘下一页’按钮,实现翻页功能

(3)在输入页数的文本框中输入页数,点击确定,实现翻页功能

def page():
    driver = login.driver
    #获取总页数
    l.findId(driver,"laypageid")
    total_pages = len(l.findsCss(driver,"a[href='javascript:;']"))
    print("total_pages is %s" %(total_pages))
    #点击下一页
    for i in range(total_pages-1):
        l.findClassName(driver,"next").click()                  #next:下一页的class name
        time.sleep(2)

    #点击上一页
    for i in range(total_pages-1):
        l.findClassName(driver,"prev").click()                  #prev:上一页的class name
        time.sleep(2)

    #翻页输入框中输入页数点击
    for i in range(1,total_pages+1):
        l.findClassName(driver,"textboxid").send_keys(i)
        l.findClassName(driver,"laypage_btn").click()
        time.sleep(2)

 以上的代码并不算是实现了翻页的功能,仅仅是为了记录下在翻页功能的实现上遇到的困难,以便后期重新捡起修改。

相关文章:

  • 2021-12-29
  • 2021-12-26
  • 2022-01-07
  • 2021-06-17
  • 2022-02-27
  • 2022-12-23
猜你喜欢
  • 2021-07-10
  • 2021-05-10
  • 2021-12-12
  • 2021-09-15
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案