Invariance of the electromagnetic field in static spacetimes