Python selenium模块模拟微信投票

Python selenium模块模拟微信投票

因为这个链接里面都是选择题和填空题,所以代码会比较简单

直接上代码:

import random

import xlrd
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time

def vote(username, work_num, iphone_no, list1, list2, list3, list4, driver):
# driver = webdriver.Firefox()
#
# # if not count:
# driver.get(“https://*******/Survey2/?id=61”)
stop_time = 0.1
stop_time1 = 2
refresh_time = 1
time.sleep(stop_time1)
# 姓名
# driver.find_element_by_id(“userName”).send_keys(“张伟”)
driver.find_element_by_id(‘userName’).send_keys("{}".format(username))
time.sleep(stop_time)
# 工号
driver.find_element_by_id(“workerNum”).send_keys("{}".format(work_num))
time.sleep(stop_time)
# 手机号
driver.find_element_by_id(“phoneNum”).send_keys("{}".format(iphone_no))
time.sleep(stop_time)

# 选题1
driver.find_element_by_xpath('//input[@value="1" and @name="1558937665656"]').click()
time.sleep(stop_time)
# 选题2
driver.find_element_by_xpath('//input[@value="1" and @name="1558945701089"]').click()
time.sleep(stop_time)

# 选题13
driver.find_element_by_xpath('//input[@value="{}" and @name="1558958477711"]'.format(list3[0])).click()
time.sleep(stop_time)
driver.find_element_by_xpath('//input[@value="{}" and @name="1558958477711"]'.format(list3[1])).click()
time.sleep(stop_time)
driver.find_element_by_xpath('//input[@value="{}" and @name="1558958477711"]'.format(list3[2])).click()
# time.sleep(1)
# driver.find_element_by_xpath('//input[@value="3" and @name="1558958477711"]').click()
# time.sleep(1)
# driver.find_element_by_xpath('//input[@value="4" and @name="1558958477711"]').click()
# time.sleep(1)
# driver.find_element_by_xpath('//input[@value="5" and @name="1558958477711"]').click()
# time.sleep(1)
# driver.find_element_by_xpath('//input[@value="6" and @name="1558958477711"]').click()

# 选题14
driver.find_element_by_xpath('//input[@value="1" and @name="1558958477713"]').click()
time.sleep(stop_time)

# 选题15
driver.find_element_by_xpath('//input[@value="{}" and @name="1558958477714"]'.format(list4[0])).click()
# time.sleep(stop_time)
driver.find_element_by_xpath('//input[@value="{}" and @name="1558958477714"]'.format(list4[1])).click()
driver.find_element_by_xpath('//input[@value="{}" and @name="1558958477714"]'.format(list4[2])).click()
time.sleep(stop_time)

# 选题16
driver.find_element_by_xpath('//input[@value="{}" and @name="1558958477715"]'.format(list4[0])).click()
# time.sleep(stop_time)
driver.find_element_by_xpath('//input[@value="{}" and @name="1558958477715"]'.format(list4[1])).click()
# time.sleep(stop_time)
driver.find_element_by_xpath('//input[@value="{}" and @name="1558958477715"]'.format(list4[2])).click()
time.sleep(stop_time)

# 选题17
driver.find_element_by_xpath('//input[@value="{}" and @name="1558958477716"]'.format(list4[0])).click()
# time.sleep(stop_time)
driver.find_element_by_xpath('//input[@value="{}" and @name="1558958477716"]'.format(list4[1])).click()
# time.sleep(stop_time)
driver.find_element_by_xpath('//input[@value="{}" and @name="1558958477716"]'.format(list4[2])).click()
# time.sleep(stop_time)
driver.find_element_by_xpath('//input[@value="{}" and @name="1558958477716"]'.format(list4[3])).click()
time.sleep(stop_time)

# 选题18
driver.find_element_by_xpath('//input[@value="{}" and @name="1558958477717"]'.format(list1[0])).click()
time.sleep(stop_time)

# 选题19
driver.find_element_by_xpath('//input[@value="{}" and @name="1558958477718"]'.format(list1[0])).click()
time.sleep(stop_time)

# 选题20
driver.find_element_by_xpath('//input[@value="{}" and @name="1558958477719"]'.format(list1[0])).click()
# time.sleep(stop_time)

# 选题21
driver.find_element_by_xpath('//input[@value="{}" and @name="1558958477720"]'.format(list1[0])).click()
time.sleep(stop_time)

# 选题22
driver.find_element_by_xpath('//input[@value="3" and @name="1558958477721"]').click()
time.sleep(stop_time)

# 选题23
driver.find_element_by_xpath('//input[@value="0" and @name="1558958477722"]'.format(list1[0])).click()
time.sleep(stop_time)

# 填空题24
# driver.find_element_by_class_name("fill").send_keys("覆盖刀具这个生命周期")
# driver.find_element_by_xpath('//div[@index="24" and @class="fill-box" and @class="fill"]').send_keys("覆盖刀具这个生命周期")
# driver.find_element_by_xpath('//div[@index="24"]/div[@class="fill-box"]/textarea').send_keys("没有")
# time.sleep(stop_time)

# 选题25
driver.find_element_by_xpath('//input[@value="1" and @name="1558958477724"]').click()
time.sleep(stop_time)

# 填空题26
# driver.find_element_by_xpath('//div[@index="26"]/div[@class="fill-box"]/textarea').send_keys()
time.sleep(stop_time)
#
# 填空题27
# driver.find_element_by_xpath('//div[@index="27"]/div[@class="fill-box"]/textarea').send_keys("无")
# time.sleep(stop_time)
#
# # 填空题28
# driver.find_element_by_xpath('//div[@index="28"]/div[@class="fill-box"]/textarea').send_keys("无")
# time.sleep(stop_time)
#
# # 填空题29
# driver.find_element_by_xpath('//div[@index="29"]/div[@class="fill-box"]/textarea').send_keys("无")
# time.sleep(stop_time)

# 模拟点击登录
driver.find_element_by_xpath("//span[@class='qsbtn submit']").click()
time.sleep(stop_time)

# 等待3秒
time.sleep(refresh_time)

# 生成登陆后快照
driver.save_screenshot(u"vote.png")

# driver.back()
#
# driver.forward()
#
driver.refresh()
# driver.quit()

def getUserMsg():
filename = “ST RENYUAN.xlsx”
# 读取数据
data = xlrd.open_workbook(filename)
# 获取sheet的数量
# page =len(data.sheets())
# 获取第一张表
table1 = data.sheets()[0]
# 获取总行数
# nrows = table1.nrows
# 获取总列数
# ncols = table1.ncols
# print(nrows, ncols)
# print(page)

for i in range(200, 210):
    # print(table1.row_values(i)[1])
    yield table1.row_values(i)[1], table1.row_values(i)[2]

def getRandomData(list_, n):
return random.sample(list_, n)

if name == “main”:
a = getUserMsg()
list1 = [1, 2]

# 选项为4个的选题
list2 = [1, 2, 3]

# 选项为5个的选题
list3 = [1, 2, 3, 4, 5]

# 选项为大于等于7个的选题
list4 = [2, 3, 4, 5, 6, 7]

# 生成电话号码
head = ["187", "159", "150", "137"]
list5 = ['0','1','2','3','4','5','6','7','8','9']

users = getUserMsg()

count = 0
driver = webdriver.Firefox()

# if not count:
driver.get("https://iedu.foxconn.com/Survey2/?id=61")
for user in users:
    iphone_no = getRandomData(head,1)+getRandomData(list5,8)
    iphone_no_ = ''.join(iphone_no)
    list1_ = getRandomData(list1,1)
    list2_ = getRandomData(list2, 2)
    list3_ = getRandomData(list2, 3)
    list4_ = getRandomData(list4, 4)
    count +=1

    vote(user[1], user[0], iphone_no_, list1_, list2_, list3_, list4_, driver)


driver.quit()

刚学Python爬虫,希望大家多给意见,欢迎加QQ2954538230一起讨论

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值