1

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 the parent_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.

Chandral
  • 448
  • 1
  • 3
  • 19
  • Can you try use it in try except block ? `while true: try: actionchains.context_click(parent_folder).perform() break except: print("failed")` –  Jun 01 '17 at 08:53
  • Ok I tried that and it still failed. – Chandral Jun 01 '17 at 09:12
  • @Chandral Can you please add full code with your loop? – Chanda Korat Jun 01 '17 at 09:44
  • @ChandaKorat I have added the code snippet with the loop. Thanks. – Chandral Jun 01 '17 at 10:37
  • 1. Can you sleep(1) between click and context click ? 2. situation is same in different browsers ? 3. if your try block fails, it's always will execute same code for dealing with same elements. no matter which folder from folder_path_list in the loop. Is it intended ? –  Jun 02 '17 at 00:13
  • Thanks @SergeiZ but the answer provided by Amit below works perfect. – Chandral Jun 02 '17 at 09:51

1 Answers1

2

Where is the actionchains initialized? Try reinitializing the action chains before using it in the except loop

actionchains = ActionChains(browser)
actionchains.context_click(parent_folder).perform()
Amit
  • 19,780
  • 6
  • 46
  • 54
  • Thanks, that works. Why would I have to initialize the variable in each loop? – Chandral Jun 02 '17 at 09:52
  • 1
    When you call methods for actions on the ActionChains object, the actions are stored in a queue in the ActionChains object. When you call perform(), the events are fired in the order they are queued up. StaleElementReferenceException means it is trying to access an old reference. – Amit Jun 02 '17 at 12:22