简体   繁体   中英

Python-Selenium error: StaleElementReferenceException

I'm using selenium with python, my main function calls are:

driver = webdriver.Firefox()
actions = ActionChains(driver)
admin = AdminActions()

admin.log_in_as_admin(driver, actions)
admin.create_test_user(driver, actions)
admin.delete_test_user(driver, actions)
admin.log_out_from_admin(driver, actions)

................................................................................

my problem is, that if I'm running it separatedly like:

admin.log_in_as_admin(driver, actions)
admin.log_out_from_admin(driver, actions)

or

admin.log_in_as_admin(driver, actions)
admin.create_test_user(driver, actions)

etc... but when i try to run more, like

admin.log_in_as_admin(driver, actions)
admin.create_test_user(driver, actions)
admin.delete_test_user(driver, actions)
admin.log_out_from_admin(driver, actions)

it throws an exeption message: Element not found in the cache.

Here's the AdminActions class:

class AdminActions():

def log_in_as_admin(self, driver, actions):
    driver.get("http://192.168.30.128:8080/reviewboard/account/login/?next=/reviewboard/dashboard/")
    user_name = driver.find_element_by_id("id_username").send_keys("admin")
    user_pwd = driver.find_element_by_id("id_password").send_keys("adminpwd")
    log_in_menu = driver.find_element_by_xpath("//*[@id=\"login_form\"]/div[3]/div/input").click()

def create_test_user(self, driver, actions):
    driver.get("http://192.168.30.128:8080/reviewboard/dashboard/")
    user_menu = driver.find_element_by_xpath('//*[@id="accountnav"]/li[2]/a')
    admin_button = driver.find_element_by_xpath('//*[@id="accountnav"]/li[2]/ul/li[2]/a')
    actions.move_to_element(user_menu).click(admin_button).perform()
    add_user_button = driver.find_element_by_xpath('//*[@id="widget-manage"]/div/table/tbody/tr[1]/td[2]/a/div').click()
    user_name = driver.find_element_by_id("id_username").send_keys("test_user")
    user_password = driver.find_element_by_id("id_password1").send_keys("secret_password")
    user_password = driver.find_element_by_id("id_password2").send_keys("secret_password")
    review_type_button = driver.find_element_by_xpath('//*[@id="id_schedule_set-0-reviewtypes_0"]').click()
    review_type_button = driver.find_element_by_xpath('//*[@id="id_schedule_set-0-reviewtypes_1"]').click()
    review_type_button = driver.find_element_by_xpath('//*[@id="id_schedule_set-0-reviewtypes_2"]').click()
    review_type_button = driver.find_element_by_xpath('//*[@id="id_schedule_set-0-reviewtypes_3"]').click()
    review_type_button = driver.find_element_by_xpath('//*[@id="id_schedule_set-0-reviewtypes_4"]').click()
    review_type_button = driver.find_element_by_xpath('//*[@id="id_schedule_set-0-reviewtypes_5"]').click()
    review_type_button = driver.find_element_by_xpath('//*[@id="id_schedule_set-0-reviewtypes_6"]').click()
    review_type_button = driver.find_element_by_xpath('//*[@id="id_schedule_set-0-reviewtypes_7"]').click()
    review_type_button = driver.find_element_by_xpath('//*[@id="id_schedule_set-0-reviewtypes_8"]').click()
    driver.find_element_by_xpath('//*[@id="user_form"]/div/div[2]/input[1]').click()

def delete_test_user(self, driver, actions):
    driver.get("http://192.168.30.128:8080/reviewboard/dashboard/")
    user_menu = driver.find_element_by_xpath('//*[@id="accountnav"]/li[2]/a')
    admin_button = driver.find_element_by_xpath('//*[@id="accountnav"]/li[2]/ul/li[2]/a')
    actions.move_to_element(user_menu).click(admin_button).perform()
    users_button = driver.find_element_by_xpath('//*[@id="widget-manage"]/div/table/tbody/tr[1]/th/a')
    users_button.click()
    search_bar = driver.find_element_by_id("searchbar")
    search_bar.send_keys("test_user")
    search_button = driver.find_element_by_xpath('//*[@id="changelist-search"]/div/input[2]')
    search_button.click()
    driver.find_element_by_xpath('//*[@id="changelist-form"]/div[1]/label/select/option[2]').click()
    driver.find_element_by_xpath('//*[@id="result_list"]/tbody/tr/td[1]/input').click()
    driver.find_element_by_xpath('//*[@id="changelist-form"]/div[1]/button').click()
    driver.find_element_by_xpath('//*[@id="content"]/form/div/input[4]').click()            

def log_out_from_admin(self, driver, actions):
    driver.get("http://192.168.30.128:8080/reviewboard/dashboard/")
    quit_menu = driver.find_element_by_xpath('//*[@id="accountnav"]/li[2]/a')
    quit_button = driver.find_element_by_xpath('//*[@id="accountnav"]/li[2]/ul/li[3]/a')
    actions.move_to_element(quit_menu).click(quit_button).perform()

Try adding an explicit wait to retry with a try..except block as shown below to handle StaleElementReferenceException .

for i in xrange(30):
    try:
      --> line of code throwing the exception <--
      break
    except StaleElementReferenceException:
      print 'attempting to recover from StaleElementReferenceException ...'
      time.sleep(1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM