0

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()
user3587233
  • 253
  • 1
  • 2
  • 13
  • Because you have navigated away from the page where your list was built their reference in memory is now invalid - this means you'll need to rebuild the list each time you return to the page. – Mark Rowlands Oct 16 '14 at 08:14
  • possible duplicate of [How to Navigate to a New Webpage In Selenium?](http://stackoverflow.com/questions/24775988/how-to-navigate-to-a-new-webpage-in-selenium) – SiKing Oct 16 '14 at 19:48

1 Answers1

5

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)
Mark Rowlands
  • 5,357
  • 2
  • 29
  • 41
  • I have a different code where I am using the same above technique since I have to navigate back to the previous page. In my new code I am finding "elements" and hover the mouse over each element then click on the "Table Name Link" to navigate to a different page for validation then again come back to the first page click on the second element and click on the second "table name link". Issue is that for some reason my code is only clicking on the table name link for the first time and for the second time the mouse hover is done correctly but the click on the table name link is not working. – user3587233 Nov 04 '14 at 17:09