until获取元素失效
2018-07-18 本文已影响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)