In order to detect the Cross-Site Script (XSS)vulnerabilities in the web applications, this paper proposes a method of XSS vulnerability detection using optimal attack vector repertory. This method generates an attack vector repertory automatically, optimizes the attack vector repertory using an optimization model, and detects XSS vulnerabilities in web applications dynamically. To optimize the attack vector repertory, an optimization model is built in this paper with a machine learning algorithm, reducing the size of the attack vector repertory and improving the efficiency of XSSvulnerability detection. Based on this method, an XSSvulnerability detector is implemented, which is tested on 50real-world websites. The testing results show that the detector can detect a total of 848 XSS vulnerabilities effectively in 24 websites.