How to Navigate to a New Webpage In Selenium?

Turns out you need to store the links you want to navigate to in advance. This is what ended up working for me (found this thread to be helpful):

driver.get(<some url>)
elements = driver.find_elements_by_xpath("//h2/a")

links = []
for i in range(len(elements)):
    links.append(elements[i].get_attribute('href'))

for link in links:
    print 'navigating to: ' + link
    driver.get(link)

    # do stuff within that page here...

    driver.back()

Leave a Comment