I am automating the task of creating a folder structure on our web application and it requires to right-click and open the context menu from where I can add a new folder.
folder_path = "Folder A\Folder B\Folder C"
folder_path_list = folder_path.split('\\')
for folder in folder_path_list:
try:
folder = WebDriverWait(browser, 15).until(EC.element_to_be_clickable((By.CSS_SELECTOR,
"div.tree-row.selected + div.tree-children > div > div > a[title='{}']".format(folder))))
folder.click()
time.sleep(5)
except:
parent_folder = WebDriverWait(browser, 8).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.tree-row.selected > a > span.tree-label")))
parent_folder.click()
actionchains.context_click(parent_folder).perform()
add_new_folder_icon = WebDriverWait(browser, 8).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "li.CMicon-add-location")))
add_new_folder_icon.click()
del parent_folder
This code runs for each folder in a loop which executes successfully during the first iteration but fails to perform the right-click on line 3 for the second iteration. It throws a StaleElementReferenceException error. I have tried the following solutions with no success
- Added the
del
command to clear theparent_folder
variable at the end of each loop. - Refreshing the whole browser page and re-navigate to the last folder I created and yet it throws the same exception.
What confuses me is that it faces problem only to perform the right-click even though it is able to find the element and do a left-click on it and select it.