Python Selenium Webdriver在iframe上的循环

Python Selenium Webdriver在iframe上的循环,python,selenium,web-scraping,webdriver,Python,Selenium,Web Scraping,Webdriver,我正在尝试创建一个包含链接列表的iframe对象。最终目标是执行一系列操作,每个链接都相同,从而以xls格式下载一些数据 对于iframe中的每个链接,我希望自动执行以下操作: 点击链接 从下拉列表中选择从1999年起获取数据的选项 单击excel下载按钮 我的代码适用于第一个链接,但我正在努力使其自动化,以便遍历iframe中的所有链接 这是适用于第一个链接的代码: #this automates the log in driver.find_element_by_id('user').sen

我正在尝试创建一个包含链接列表的iframe对象。最终目标是执行一系列操作,每个链接都相同,从而以xls格式下载一些数据

对于iframe中的每个链接,我希望自动执行以下操作:

  • 点击链接
  • 从下拉列表中选择从1999年起获取数据的选项
  • 单击excel下载按钮
  • 我的代码适用于第一个链接,但我正在努力使其自动化,以便遍历iframe中的所有链接

    这是适用于第一个链接的代码:

    #this automates the log in
    driver.find_element_by_id('user').send_keys('myusername')
    driver.find_element_by_id('password').send_keys('mypassword')
    driver.find_element_by_xpath('myxpath').click()
    
    #this switches to iframe and clicks on all the links 
    driver.switch_to.frame("report-iframe")
    driver.find_element_by_tag_name('a').click()
    
    #this performs the required actions for the first link(select drop down, select excel download)  
    s= Select(driver.find_element_by_id('start-date'))
    s.select_by_value('1999-01-01')
    
    driver.find_element_by_xpath('//*[@id="report-excel-download"]')
    
    wait = WebDriverWait(driver, 10)
    element = wait.until(EC.element_to_be_clickable((By.ID, 'report-excel-download')))
    element.click()
    
    我想为iframe中的所有链接自动执行此过程(即,对于iframe中的每个链接,单击链接,选择下拉列表,单击数据下载按钮。是否有人对此有任何建议

    我试过:

    links=[link.get_attribute('href') for link in driver.find_elements_by_xpath('myxpath')]
    
    
    for l in links:
    
        #click the link
        driver.find_element_by_tag_name('a').click()
    
        #select all years
        s= Select(driver.find_element_by_id('report-control-date-start'))
        s.select_by_value('1999-01-01')
    
        #click download
        driver.find_element_by_xpath('//*[@id="report-excel-download"]')
        wait = WebDriverWait(driver, 10)
        element = wait.until(EC.element_to_be_clickable((By.ID, 'report-excel-download')))
        element.click()
    
    但这只适用于iframe中的第一个链接,并不适用于所有链接

    有人有什么建议吗


    谢谢!

    切换到每个帧,然后切换回默认值。上面有详细的答案。当你点击链接时会发生什么?将你重定向到另一页,打开弹出窗口?当我点击链接时,它会将我重定向到另一页。所以在你的循环中,你点击一个链接,它会将你带到一个新页,并在那里做一些事情。在什么时候int您是否返回到链接页面?切换到每个帧,然后切换回默认值。上面有详细的答案。当您单击链接时会发生什么?将您重定向到另一个页面,打开弹出窗口?当我单击链接时,它会将我重定向到另一个页面。因此,在您的循环中,您可以单击链接,将您带到新页面,并执行一些操作你什么时候回到链接页面?