How to Navigate to a New Webpage In Selenium?

前端 未结 2 1454
天命终不由人
天命终不由人 2020-11-27 22:26

I have the following code:

driver.get()
for element in driver.find_elements_by_class_name(\'thumbnail\'):
    element.find_element_by_xpath(\         


        
2条回答
  •  庸人自扰
    2020-11-27 23:13

    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()
    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()
    

提交回复
热议问题