Python调用Selenium模拟Chrome浏览器操作

接入网络前要登录认证,而且每天都要登录一次实在麻烦,
最要命的是尝试了多种方法,包括curl、postman发请求、powershell调用IE模拟操作都存在失败的可能,发现失败的原因都和cookie有关,也尝试了从请求中抽取cookie数据仍然失败。

Powershell模拟IE虽然没什么问题,但得益于微软一贯的病入膏肓,要么不修改注册表就没法调用IE,要么就是运行了一两次就报莫名其妙的错误,微软怎么这么不争气。

终于找到了Selenium这个救星,把人工操作脚本化总没问题了嘛。

# coding:utf-8
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import requests
import sys
import datetime

orig_stdout = sys.stdout
f = open("auth.log", 'w')
sys.stdout = f

#oa auth
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
browser = webdriver.Chrome(chrome_options=chrome_options)
login_url = "http://20.0.0.2/site3/e4db7920330c4b3f86700d35f080c5b6/login.html"
userName = "xxx"
password = "xxx"
#initial login
print(str(datetime.datetime.now()) + " OA Initializing")
browser.get(login_url)
# browser.delete_cookie('cmp_glb_param')
browser.find_element_by_id("account").send_keys(userName)
browser.find_element_by_id("acc_pass").send_keys(password)
browser.find_element_by_id("account_login_checkbox").click()
browser.find_element_by_id("account_login_btn").click()
# wait for URL to change with 5 seconds timeout
WebDriverWait(browser, 5).until(EC.url_changes(login_url))
cur_url = browser.current_url
#verify
if cur_url.find("ad.html") != -1:
    print(str(datetime.datetime.now()) + " Login SUCCESS")
else:
    if cur_url.find("login.html") != -1:
        print(datetime.datetime.now() + " Login again")
        browser.find_element_by_id("account").send_keys(userName)
        browser.find_element_by_id("acc_pass").send_keys(password)
        browser.find_element_by_id("account_login_checkbox").click()
        browser.find_element_by_id("account_login_btn").click()
    elif cur_url.find("login_unfirst.html") != -1:
        print(str(datetime.datetime.now()) + " Login unfirst")
        browser.find_element_by_id("input_checkbox").click()
        browser.find_element_by_id("login_btn").click()
    #check status
    WebDriverWait(browser, 5).until(EC.url_changes(cur_url))
    cur_url = browser.current_url
    if cur_url.find("ad.html") != -1:
        print(str(datetime.datetime.now()) + " Login SUCCESS")
    else:
        print(str(datetime.datetime.now()) + " Login FAILED")
browser.quit()

#dev auth
cookies = {
    'ac_login_info': 'passwork',
}
headers = {
    'Accept': 'text/html, application/xhtml+xml, image/jxr, */*',
    'Referer': 'http://20.3.4.2/webAuth/',
    'Accept-Language': 'zh-CN',
    'Content-Type': 'application/x-www-form-urlencoded',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko',
    'Accept-Encoding': 'gzip, deflate',
    'Host': '20.3.4.2',
    'Connection': 'Keep-Alive',
    'Pragma': 'no-cache',
}
data = {
  'username': userName,
  'password': 'xxx',
  'rememberPwd': '1',
  'pwd': 'xxx',
  'secret': 'true'
}
#send
print(str(datetime.datetime.now()) + " Dev Initializing")
response = requests.post('http://20.3.4.2/webAuth/', headers=headers, cookies=cookies, data=data, verify=False)
print(str(datetime.datetime.now()) + " Dev Result:" + str(response.status_code))

sys.stdout = orig_stdout
f.close()

你可能感兴趣的:(Python调用Selenium模拟Chrome浏览器操作)