Of course! Planes are on autopilot for most of the time, and the co-pilot's there if anything happens. The pilot (or captain as they're called) is able to use the bathroom without any issues. Some airlines even opt to have a special bathroom for the crew installed.