selenium+chrome headless完成自动百度八斗平台的录入问题(python版本为2.7)

# -*- coding: utf-8 -*-  
import sys
reload(sys)
sys.setdefaultencoding('utf-8')

import os
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
import time
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument("window-size=1024,768")
chrome_options.add_argument("--no-sandbox")

base_url = "http://badou.baidu.com/login"
#对应的chromedriver的放置目录
driver = webdriver.Chrome(executable_path=('/usr/local/nginx/html/selenium/chromedriver'), chrome_options=chrome_options)
#打开url
driver.get(base_url + "/")
#开始
start_time=time.time()
print('this is start_time ',start_time)

#登录 输入框
userinput_js = "var userName_node = document.getElementsByName('userName')[0];var password_node = document.getElementsByName('password')[0];userName_node.value = '你的账号';password_node.value = '你的密码';"
print(userinput_js)
driver.execute_script(userinput_js)
#登录 确认
login_js = "document.getElementsByTagName('input')[18].click();"
print(login_js)
driver.execute_script(login_js)
#睡眠3秒 截图
time.sleep(3)
driver.save_screenshot('screen.png')
#验证码
print('写上你自己的图片地址url')
imgcode = raw_input('请输入看到的验证码:')
verify_input_js = "var verify_node = document.getElementsByName('verifyCode')[0];verify_node.value='"+imgcode+"'"
driver.execute_script(verify_input_js)
#截图 查看步骤
driver.save_screenshot('screen.png')
print('写上你自己的图片地址url')
tmp=raw_input('输入任意值继续.')
#登录之后跳转
print('跳转开始...')
jump_js = "var pass_btn = document.getElementsByClassName('pass-button pass-button-submit')[0];pass_btn.click();"
driver.execute_script(jump_js)
time.sleep(3)
close_js = "document.getElementsByClassName('el-dialog__close el-icon el-icon-close')[0].click()"
driver.execute_script(close_js)
driver.save_screenshot('screen.png')
#动态令牌
verify = raw_input('请输入动态令牌:')
verify_js = "var verify_node = document.getElementsByClassName('forceverify-input forceverify-input-vcode')[1];verify_node.value='"+verify+"'"
driver.execute_script(verify_js)
#最终登录
jump_js = "document.getElementsByClassName('forceverify-button forceverify-button-submit')[4].click();"
driver.execute_script(jump_js)
time.sleep(3)
driver.save_screenshot('screen.png')
#录入 问题
with open('./badou.txt','r') as f:
    line = f.readline()
    title = line
    title = title.strip()
    commit_js = "var object2str = function(obj){var str_arr = [];var i = 0;for(var key in obj){str_arr[i] = key+'='+obj[key]; i++;}return str_arr.join('&');};var ajax_post = function (url, data, fn) {var xhr = new XMLHttpRequest();xhr.open('POST', url, true);xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');  xhr.onreadystatechange = function()"+" {"+"if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 304)) {fn.call(this, xhr.responseText);}"+"};console.log(object2str(data));xhr.send(object2str(data));};var post_url = 'http://badou.baidu.com/api/question/insertUpdate';var data = {'class_id':16,'content':'','cover':'[]',ext:{"+"},'qr_type':6,'title':'"+title+"'};ajax_post(post_url,data,function(res){var string = JSON.stringify(res);var data1 = {'msg' : '问题标题:'+data.title+'返回结果:'+string};var url = 'http://ad.adstart.cn/log_in.php';ajax_post(url,data1,function(res1){console.log(res1);})});document.getElementsByTagName('a')[2].click();"
    # driver.execute_script(commit_js)
    print(commit_js)
    time.sleep(10)
f.close()
#关闭driver
driver.close()
#结束
end_time=time.time()
print('this is end_time ',end_time)

你可能感兴趣的:(selenium,python)