When Allaah is testing you with a difficulty, there’s absolutely nothing that you can do to escape it or avoid it. In fact, trying to use will power to get out of the difficulty will only intensify the difficulty because you’re adding resistance. You are meant to learn and gain something from this experience, and Allaah wouldn’t allow you to leave without having the favour completed. He never allows difficulty or evil to take place without there being a greater good counteracting it. His mercy is keeping you in place so that you can feel the benefit of the trial and become whole.
Yusuf was kept in prison longer than expected, even though he sent out a message with the freed prisoners to remind the wardens of his release. Allaah kept him there until the king had that dream that no one but him could decipher, and until he had enough leverage to make that woman who accused him come clean and absolve him of the false crime. Had he left when he wanted to, nothing but a couple of years in prison would have come out of that experience and he wouldn’t have anything to make the woman confess.