在獲取分頁商品信息時蒲障,獲取不到元素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)