user3587233
user3587233

Reputation: 263

How to resolve issue --- Element not found in the cache - perhaps the page has changed since it was looked up

I am trying to find set of elements then click on each element which takes me to a new page and perform some steps on that new page. Then click on the back button on the browser or a button on the new page that takes me to the previous page then find the same elements and repeat the above process for the rest of the elements.

I am using the below code to find the elements again before proceeding to find the elements but my code isn't working. Can someone please help?

      elements = driver.find_elements_by_css_selector("#top-tables-chart-container > div > svg > g > g > rect")
      counter = 0
      for counter in range(counter, len(elements)):
              elements = driver.find_elements_by_css_selector("#top-tables-chart-container > div > svg > g > g > rect")
              webdriver.ActionChains(driver).move_to_element(elements[counter]).click().perform()
              time.sleep(5)
              tableNameLink= elements[counter].find_element_by_xpath("//div[@class='d3-tip bar-chart top-tables-tooltip n']//div[@class='left-section']//div[@class='table-name']//a[contains(@href,'#/table/')]")
              print tableNameLink
              tableNameLink.click()
              tableName = driver.find_element_by_xpath("//div[@class='discover-design-transform-container clearfix']//div[@class='left-header-section clearfix']//div[@class='entity-info table-type']//span[@class='entity-identifier']")
              table = tableName.text
              print " Table: " + table
              print '\n'

              if table == "lineitem":
                TableAccessFreqChartInfoBadgesValidation(self.driver).test_table_access_freq_chart_info_badges_validation("F","8","13","13")
                time.sleep(1)
                print '\n'

              if table == "orders":
                  TableAccessFreqChartInfoBadgesValidation(self.driver).test_table_access_freq_chart_info_badges_validation("D","4","9","9")
                  time.sleep(1)
                  print '\n'
    topUsagePatternsTab = driver.find_element_by_xpath("//div[@id='workload-level-tabs']//a[@href='#/topUsagePatterns']")
    topUsagePatternsTab.click()

Upvotes: 0

Views: 1677

Answers (1)

Mark Rowlands
Mark Rowlands

Reputation: 5453

You will need to rebuild the list each time you return to the page, you were rebuilding it at the end of your loop but your for loop referenced the original list, which is no longer valid. A simple way is to use a counter within the loop to track your position.

elements = driver.find_elements_by_xpath("//your_path")
counter = 0
for counter in range(counter, len(elements)):
    elements = driver.find_elements_by_xpath("//your_path")
    elements[counter].click()
    time.sleep(2)
    discoverPageTables = driver.find_element_by_xpath("//your_path").text
    print "Tables Found :" + discoverPageTables
    discoverPageInstanceCount = driver.find_element_by_xpath("your_path").text
    print "Instance Count Found :" + discoverPageInstanceCount
    discoverpageWorkload = driver.find_element_by_xpath("//your_path").text
    print "Workload Percentage :" + discoverpageWorkload
    discoverPageHiveCompatible = driver.find_element_by_xpath("//your_path").text
    print "Hive Compatible :" + discoverPageHiveCompatible
    discoverPageComplexity = driver.find_element_by_xpath("your_path").text
    print "Complexity :" + discoverPageComplexity
    discoverPageNormalizedComplexity = driver.find_element_by_xpath("your_path").text
    print "Normalized Complexity :" + discoverPageNormalizedComplexity
    print '\n'
    driver.back()
    time.sleep(5)

Upvotes: 5

Related Questions