This is an interesting question. Shakespeare was a commoner, not a nobleman, and certainly not close to royalty, so what could he know about the ambitions of a person hoping to become a king? These were the kinds of questions that led, in the nineteenth century, to increasingly wild conjectures that Shakespeare must have been a pseudonym for a British nobleman such as the Earl of Oxford.
However, sober scholarly voices believe that Shakespeare was Shakespeare, and if we accept the premise that a great writer can and does enter into the mind and consciousness of characters unlike himself, Shakespeare was superbly qualified to write Macbeth. This is one of his later plays, written in 1606, and follows a string of successes that go back more than two decades. More recently, he had such written plays about power and corruption as Hamlet, Othello, and King Lear. Few, if any, playwrights can brag of such a resume.