While someone else gives a more detailed answer, there's an episode of Musem Secrets: Kunsthisstorisches Museum where they talk about this.
According to this documentary, the Austrian Habsburg family kept inbreeding almost until they were dethroned, even if they had more and more difficulties every time to get an heir. You can see even in the 19th century they were still marrying first cousins.
In the case of Spain, the Habsburg dinasty ended with Charles II, but the Bourbon family kept inbreeding too. For example, Ferdinand II married his first cousin Maria Antonietta of the Two Sicilies (they had no children), then his niece Maria Isabel of Portugal (two kids, one stillborn and another lived a few months), then Maria Josepha Amalia of Saxony (they were related, although not as closely, I think; no children) and finally another niece, Maria Christina of Bourbon-Two Sicilies (two daughters.)
What I don't know is if they were aware of the risk, but didn't care enough; or if they didn't know of the risks.