How to resolve, Stale element exception? if element is no longer attached to the DOM?

后端 未结 7 1199
盖世英雄少女心
盖世英雄少女心 2020-11-30 23:01

I have a question regarding \"Element is no longer attached to the DOM\".

I tried different solutions but they are working intermittent. Please suggest a solution th

7条回答
  •  忘掉有多难
    2020-11-30 23:07

    Solutions to resolve them:

    1. Storing locators to your elements instead of references
    driver = webdriver.Firefox();
    driver.get("http://www.github.com");
    search_input = lambda: driver.find_element_by_name('q');
    search_input().send_keys('hello world\n'); 
    time.sleep(5);
    
    
    search_input().send_keys('hello frank\n') // no stale element exception
    
    1. Leverage hooks in the JS libraries used
       # Using Jquery queue to get animation queue length.
        animationQueueIs = """
        return $.queue( $("#%s")[0], "fx").length;
        """ % element_id
        wait_until(lambda: self.driver.execute_script(animationQueueIs)==0)
    
    1. Moving your actions into JavaScript injection
     self.driver.execute_script("$(\"li:contains('Narendra')\").click()");
    
    1. Proactively wait for the element to go stale
      # Wait till the element goes stale, this means the list has updated
      wait_until(lambda: is_element_stale(old_link_reference))
    

    This solution, which worked for me

提交回复
热议问题