selenium解决页面一直刷新,无法获取元素的问题

  1. 这里的场景是页面一直在刷新,脚本处于等待中没有继续往下执行。
  2. 百度之后,chrome之类的浏览器可以按esc停止刷新。
  3. 适时手动按esc键盘,脚本往下继续执行。
  4. 综上,启动了两个线程,一个线程执行_send_esc方法;另一个线程执行原始的tcpdump_go_stop方法。
  5. 注意,页面要保持在前端展示

不关注返回值:

def test_TCPdump():
    '''
    1、点击GO,等几秒钟,再点击stop
    2、检索界面是否有文件可以被下载,其大小、时间是否符合预期
    '''
    driver = gen_chrome_driver()
    # 登录
    netone_login(driver)

    click_network_tools(driver)
    click_TCPdump_tab(driver)

    # tcpdump_go_stop(driver)
    thread1 = threading.Thread(target=tcpdump_go_stop, args=(driver,))
    thread2 = threading.Thread(target=_send_esc)
    # 启动线程
    thread1.start()
    thread2.start()
    # 等待线程执行完毕
    thread1.join()
    thread2.join()

    driver.quit()


def _send_esc():
    time.sleep(3)
    pyautogui.press('esc')

 关注返回值

def test_TCPdump():
    '''
    1、点击GO,等几秒钟,再点击stop
    2、检索界面是否有文件可以被下载,其大小、时间是否符合预期
    '''
    driver = gen_chrome_driver()
    # 登录
    netone_login(driver)

    time.sleep(1)
    click_network_tools(driver)
    click_TCPdump_tab(driver)

    # tcpdump_go_stop(driver)

    with concurrent.futures.ThreadPoolExecutor() as executor:
        # 提交任务到线程池,并获取Future对象
        future1 = executor.submit(tcpdump_go_stop, driver)
        future2 = executor.submit(_send_esc)

        # 等待任务执行完成并获取返回值
        result = future1.result()
    if not result:
        assert False
    
    # thread1 = threading.Thread(target=tcpdump_go_stop, args=(driver,))
    # thread2 = threading.Thread(target=_send_esc)
    # # 启动线程
    # thread1.start()
    # thread2.start()
    # # 等待线程执行完毕
    # thread1.join()
    # thread2.join()
    
    driver.quit()


def _send_esc():
    time.sleep(3)
    pyautogui.press('esc')

 

posted @ 2023-09-28 13:30  你说夕阳很美  阅读(675)  评论(0)    收藏  举报