"It’s been cloudy and rainy in my city since last week. I live in an apartment, so sunlight is precious—it helps us dry our clothes faster. Yesterday, I saw the sun and took advantage of it by hanging up the towels."
About "take advantage"
Your usage is understandable, but in this context, native speakers would say "make the most of" or "take the opportunity to."
Your other example, corrected:
"I went to church, and on my way back home, I took the opportunity to stop by the supermarket and buy some milk."