我正在使用硒与蟒蛇, 我的主要函数调用是:
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)
............................................... .................................
我的问题是,如果我像以下那样分开运行:
admin.log_in_as_admin(driver, actions)
admin.log_out_from_admin(driver, actions)
或
admin.log_in_as_admin(driver, actions)
admin.create_test_user(driver, actions)
等等...但是当我尝试运行更多时,比如
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)
它会抛出一条exeption消息:在缓存中找不到元素。
这是AdminActions类:
类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()
答案 0 :(得分:0)
尝试添加显式等待以使用try..except
块重试,如下所示,以处理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)