美文网首页
until获取元素失效

until获取元素失效

作者: 认真点啊 | 来源:发表于2018-07-18 17:16 被阅读0次

    在获取分页商品信息时,获取不到元素input
    初始源码:

    def next_page(page_number):
        try:
            input = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,'#J_bottomPage > span.p-skip > input')))
            submit = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,'#J_bottomPage > span.p-skip > a')))
            input.clear()
            input.send_keys(page_number)
            submit.click()
            wait.until(EC.text_to_be_present_in_element((By.CSS_SELECTOR,'#J_bottomPage > span.p-num > a.curr'),str(page_number)))
            get_products()
        except TimeoutException:
            next_page(page_number)
    

    错误信息:

    element is not attached to the page document
    

    解决方法:
    捕获StaleElementReferenceException异常重新获取

    def next_page(page_number):
        try:
            try:
                input = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,'#J_bottomPage > span.p-skip > input')))
                submit = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,'#J_bottomPage > span.p-skip > a')))
                input.clear()
                input.send_keys(page_number)
                submit.click()
                #捕捉异常StaleElementReferenceException,然后重新获取input元素
            except StaleElementReferenceException:
                input = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '#J_bottomPage > span.p-skip > input')))
    
            wait.until(EC.text_to_be_present_in_element((By.CSS_SELECTOR,'#J_bottomPage > span.p-num > a.curr'),str(page_number)))
            get_products()
        except TimeoutException:
            next_page(page_number)
    

    相关文章

      网友评论

          本文标题:until获取元素失效

          本文链接:https://www.haomeiwen.com/subject/fsqapftx.html