web 自动登录的脚本
import time import os import getpass from selenium import webdriver from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC from selenium.common.exceptions import NoSuchElementException
//拼接当前用户的firefox profile temp = os.popen("dir %APPDATA%\\Mozilla\\Firefox\\Profiles\\ /b") user = str(os.popen("whoami").read().strip()).split("\\")[1] profile = "c:\\Users\\"+ user + "\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\" + str(temp.read()).strip() url = "https://vwapbjsvra01.ap.vwg/SAAS/t/VGC/auth/login?dest=" \ "https://vwapbjsvra01.ap.vwg/SAAS/auth/oauth2/authorize?response_type%3Dcode%26client_id%" \ "3Dcsp-admin-HlQkQLoJZQ%26state%3DD8D82B21ED9D4EB5306CD025C054C186%26redirect_uri%3D" \ "https://vwapbjsvra01.ap.vwg/vcac/org/vgc/#com.vmware.csp.component.ipam.service.network.profiles" name = input("Please input username:") pwd = getpass.getpass() while True: os.system("taskkill /F /IM firefox.exe") # 调用firefox浏览器,指定firefox浏览器的路径,以及浏览器的profile wd = webdriver.Firefox( firefox_profile=profile, firefox_binary="C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe") wd.get(url) wd.maximize_window() try: username = WebDriverWait(wd, timeout=5).until(EC.presence_of_element_located((By.ID, "username")), message="timeout") username.send_keys(name) password = WebDriverWait(wd, timeout=5).until(EC.presence_of_element_located((By.ID, "password")), message="timeout") password.send_keys(pwd) wd.find_element_by_id("loginFormSubmit").click() print("登录成功...") time.sleep(5) wd.find_element_by_id("cafe.work.items").click() print("进入inbox...") except NoSuchElementException as e: print(e.msg) print("等待4小时再次自动执行...") time.sleep(14400)
注意: 需要安装firefox的一个插件geckodriver 地址:https://github.com/mozilla/geckodriver/releases,插件放置位置 firefox根目录,然后将firefox目录添加到环境变量中。