Python script for parsing links I wrote a test that parses links from a web page, and in a loop, clicks on each of them, and then returns to the main page. But, each iteration is accompanied by parsing the entire web page again and again and overwriting the links array, although only one link is needed per iteration. I understand that this is inefficient. How can I optimize this?

I tried parsing the links only once and then iterating through them in a loop. After the first iteration, it goes back (to the main page) and tries to click on the second link, but it's not interactive (I think it's because of the web elements that are stored in the links array changing every time you go to the page).

def setUp(self):
    self.driver = webdriver.Chrome()
    self.driver.get('https://yandex.ru')

def test_01(self): 
    driver = self.driver
    links = []
    time.sleep(3)
    links = driver.find_elements_by_css_selector("a")
    for i in range(len(links)):
        links = driver.find_elements_by_css_selector("a")
        links[i].click()
        driver.get('https://yandex.ru')
        time.sleep(3)
When you navigate away from the page the WebElements get invalidated as DOM changes therefore I would recommend going for List Comprehension and convert the list of WebElements into the list of links text. 
Once done you should be able to use normalize-text() function just in case the text belongs to child element or has starting/trailing spaces
And last but not the least consider using Explicit Wait just in case the link is populated via AJAX call. 


Example code:

driver.get("http://yandex.ru")
links = [link.text for link in driver.find_elements_by_css_selector("a")]
for link in links:
    WebDriverWait(driver, 10) \
        .until(expected_conditions
        .presence_of_element_located(
        (By.XPATH, "//a[normalize-space()='" + link + "']"))).click()
    # do what you need here
    print(driver.title)
    driver.back()
driver.quit()