If the dishwasher begins to operate during this test, then you have isolated the problem. The door switch is not being fully depressed when the door is latched. There are three likely possibilities:
The door latch is misaligned.
The entire door is misaligned.
The striker for the door switch is damaged or misaligned.
To service the door latch, it may be necessary to access it in the control panel. Access varies by model; on some models you can remove just the control panel on others you must remove the interior or exterior panel of the door.
Before removing the control or interior panel, look first for exposed screws that may allow you to adjust the latch or the strike plate. You may be able to loosen the screws and shift the latch on the door or the strike plate on the cabinet. It may require some trial and error, but you can loosen, adjust, tighten and test until it latches properly.