I'd recommend learning conditional sleeping, this would both solve your issue and make it more responsive. Since it seems you're new to programming, you could add a 500ms sleep after your second interaction.
Also you have :
if (!getWidgets().isVisible(270, 14)) {
//do stuff
}
if (getWidgets().isVisible(270, 14)) {
//do more stuff
}
You can replace this with:
if (!getWidgets().isVisible(270, 14)) {
} else {
}
The else is faster on the machine since it's only checking that if statement once. The else is the inverse of the if statement. In human english terms, you would read this as, "if widget 270, 14 is NOT NOT visible", NOT NOT, double not so this is true, therefore "if widget 270, 14 is visible".
Edit: accidentally missed a !