const webdriver = require('selenium-webdriver'); const { Options } = require('selenium-webdriver/chrome'); const caps = webdriver.Capabilities.chrome(); //设置为开发者模式,防止被各大网站识别出来使用了Selenium caps.set('chromeOptions', { 'excludeSwitches': ['enable-automation'] }) //不加载图片,加快访问速度 caps.set('prefs', { "profile.managed_default_content_settings.images": 2 }) // 设置chrome.exe 位置 const options = new Options(); options.setChromeBinaryPath('C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe'); const userName = '18683971105'; const password = 'chengrang.' async function initDriver() { let driver = new webdriver.Builder() .setChromeOptions(options) .withCapabilities(caps) .build(); driver.executeScript(()=>{ Object.defineProperty(navigator, 'webdriver', {get: () => undefined}); }) await driver.get('https://login.taobao.com/') // await driver.get('https://login.taobao.com/member/login.jhtml?tpl_redirect_url=https%3A%2F%2Fwww.tmall.com&style=miniall&enup=true&newMini2=true&full_redirect=true&sub=true&from=tmall&allp=assets_css%3D3.0.10/login_pc.css&pms=1598493116008') const initWindow = await driver.getWindowHandle(); // 模拟登录 // const {loginUrl,account,password} = this.config.tmallConfig; // driver.get(loginUrl); await driver.sleep(1000) const accountEle = await driver.findElement(webdriver.By.xpath('//*[@id="fm-login-id"]')); const passwordEle = await driver.findElement(webdriver.By.xpath('//*[@id="fm-login-password"]')); const submitBtn = await driver.findElement(webdriver.By.xpath('//*[@id="login-form"]/div[4]/button')); await accountEle.sendKeys(userName) await passwordEle.sendKeys(password); await submitBtn.click(); return { driver, initWindow } } initDriver(); module.exports = initDriver;