使用chrome Headless实现自动完成问卷

使用chrome Headless实现自动完成问卷

简单完成毛概小作业

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')

chrome_options.binary_location = '/opt/google/chrome/chrome'


js='''void( (function() { var hash = {}; var a = document.evaluate('//input[(@type="checkbox" or @type="radio") and not(@value="0")]//@name', document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null); if (a.snapshotLength) { for (var i = 0; i < a.snapshotLength; i++) { if (! (a.snapshotItem(i).value in hash)) hash[a.snapshotItem(i).value] = 0; hash[a.snapshotItem(i).value]++; } for (i in hash) { document.evaluate('//input[(@type="checkbox" or @type="radio") and not(@value="0") and @name="' + i + '"]', document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null).snapshotItem(Math.floor(Math.random() * hash[i])).click(); } } var objs = document.getElementsByTagName("textarea"); for (var i=0;i<objs.length;i++) { objs[i].focus(); objs[i].value = "我爱学习"; objs[i].blur(); } var choose = document.getElementsByTagName("select"); for (var i=0;i<choose.length;i++) { choose[i].focus(); choose[i].value = "1"; choose[i].blur(); } }) () ) ;var btn = document.getElementById("next_button"); window.setTimeout(btn.click(),1000); '''  

for i in range(1000):
    opener = webdriver.Chrome(chrome_options=chrome_options)
    opener.get('http://www.wenjuan.in/s/yamUF3')
    opener.execute_script(js) 
    cookies = opener.get_cookies()
    opener.close()
comments powered by Disqus