如何在Python selenium中处理iframe的多个页面?

时间:2017-12-06 12:14:06

标签: python html selenium iframe

我正在与Selenium争夺一些约会。一切似乎都很好。但问题是,当尝试使用循环在iFrame的多个页面上收集数据时,我遇到的问题是driver.page_source仅返回第一页的数据。因此,循环在iframe页面之间切换,但源保持不变。

    ...
    iframe = driver.find_elements_by_tag_name('iframe')[0] #find data iframe
    driver.switch_to_frame(iframe) #switch to iframe
    pageo = 1
    while len(data)<numo:
        if pageo != 1:
            #continue
            link = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH,  '''//a[@onclick='set_current_page("'''+str(pageo) + '''");']''')))
            link.click()
            time.sleep(2)
            #driver.find_element_by_xpath(('''//a[@onclick='set_current_page("'''+str(pageo) + '''");']''')).click()
        #time.sleep(0.5)
        html_source = driver.page_source
        soup = BeautifulSoup(html_source, "html.parser")
        table = soup.findAll('table')[1]
        table_body = table.find('tbody')
        rows = table_body.find_all('tr')
        for row in rows:
            cols = row.find_all('td')
            cols = [ele.text.strip() for ele in cols]
            data.append([ele for ele in cols if ele])
            dynamic_data_entry()
        pageo = pageo + 1
    ...

这里有什么问题?

0 个答案:

没有答案