Automatic visual localization of electric vehicle (EV) charging ports presents significant challenges in uncertain environments, such as varying surface textures, reflections, lighting and observation distance. Existing methods require extensive real-world training data and well-focused images to achieve robust and accurate localization. However, both requirements are difficult to meet under variable and unpredictable conditions. This paper proposes a 2-stage vision-based localization approach. Firstly, the image synthesis technique is used to reduce the cost of real-world data collection. A task-oriented parameterization protocol (TOPP) is proposed to optimize the quality of the synthetic images. Secondly, an autofocus and servoing strategy is proposed. A hybrid detector is employed to enhance sharpness assessment performance, while a visual servoing method based on single exponential smoothing (SES) is developed to enhance stability and efficiency during the search process. Experiments were conducted to evaluate image synthesis efficiency, detection accuracy, and servoing performance. The proposed method achieved 99% detection accuracy on the real-world port images, and guided the robot to the optimal imaging position within 16 s, outperforming comparable approaches. These results highlight its potential for robust automated charging in real-world scenarios.