I've written a test that analyzes links from a webpage and then clicks on each of them in a loop and then returns to the main page. However, every iteration repeatedly parses the entire webpage and overwrites the links array. However, only one link is required per iteration. I understand that this is inefficient. How can I optimize this?
I tried to parse the links only once and then loop them through. After the first iteration, it returns (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 and change each time, though) You to switch 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)
I expect a more efficient solution.