从问卷星网站找的一个模板
编写思路大致是这样
谷歌浏览器驱动器打开问卷网址https://www.wjx.cn/jq/82201411.aspx,
xpath定位网页元素 driver.find_element_by_xpath
对元素进行操作,有点击click和填入send_keys
最后点击提交问卷//*[@id="submit_button]结束
若想要多份问卷写个循环套外面即可
注意事项
要下载相应版本的Chormedriver放在python文件夹里做驱动
填写问卷时,定位元素只能定位到可视化区域,看不见的需要将浏览器向下拉动,
js="var q=document.documentElement.scrollTop=800" #下拉像素(800是基于最顶端测算的距离) driver.execute_script(js) #执行下拉像素操作
网页定位元素操作步骤
F12弹框开发者界面,左键单击左上角标志
选择定位元素,鼠标左键单击
右键灰色区域
Copy->CopyXPath 复制到粘贴板
from selenium import webdriverimport randomimport timedef selection(*a): #单选题 输入参数为任意个数str(xpath) 返回值为某一随机str(xpath) n = len(a) num = random.randint(1,n) for i in range(1,n+1,1): if(i==num): return a[num-1]hours = selection('5', '10', '15', '20')money = str(random.uniform(1500, 2500))time = selection('一个月','两个月','三个月','四个月','五个月','六个月','一年','两年','三年')month_money = selection('200','500','1000','1500','2000','2500')def time_write(num): for i in range(num): driver = webdriver.Chrome() driver.get('https://www.wjx.cn/jq/82201411.aspx') #第一题 driver.find_element_by_xpath(selection('//*[@id="divquestion1"]/ul/li[1]/a', '//*[@id="divquestion1"]/ul/li[2]/a', '//*[@id="divquestion1"]/ul/li[3]/a', '//*[@id="divquestion1"]/ul/li[4]/a')).click() #第二题 driver.find_element_by_xpath(selection('//*[@id="divquestion2"]/ul/li[1]/a', '//*[@id="divquestion2"]/ul/li[2]/a')).click() #第三题 driver.find_element_by_xpath('//*[@id="q3"]').send_keys(money) #第四题 source = selection('父母给予','兼职获得','奖学金') driver.find_element_by_xpath('//*[@id="q4"]').send_keys(source) js="var q=document.documentElement.scrollTop=800" #下拉像素(800是基于最顶端测算的距离) driver.execute_script(js) #执行下拉像素操作 #第五题 driver.find_element_by_xpath(selection('//*[@id="divquestion5"]/ul/li[1]/a', '//*[@id="divquestion5"]/ul/li[2]/a', '//*[@id="divquestion5"]/ul/li[3]/a', '//*[@id="divquestion5"]/ul/li[4]/a')).click() #第六题 driver.find_element_by_xpath('//*[@id="q6"]').send_keys(time) #第七题 driver.find_element_by_xpath('//*[@id="q7"]').send_keys(month_money) #第八题 driver.find_element_by_xpath(selection('//*[@id="divquestion8"]/ul/li[1]/a', '//*[@id="divquestion8"]/ul/li[2]/a', '//*[@id="divquestion8"]/ul/li[3]/a', '//*[@id="divquestion8"]/ul/li[4]/a')).click() driver.find_element_by_xpath(selection('//*[@id="divquestion8"]/ul/li[5]/a', '//*[@id="divquestion8"]/ul/li[6]/a', '//*[@id="divquestion8"]/ul/li[7]/a')).click() js="var q=document.documentElement.scrollTop=1600" driver.execute_script(js) #第九题 driver.find_element_by_xpath(selection('//*[@id="divquestion9"]/ul/li[2]/a')).click() #第十题 driver.find_element_by_xpath(selection('//*[@id="divquestion10"]/ul/li[1]/a', '//*[@id="divquestion10"]/ul/li[2]/a', '//*[@id="divquestion10"]/ul/li[3]/a')).click() #第十一题 driver.find_element_by_xpath(selection('//*[@id="divquestion11"]/ul/li[1]/a', '//*[@id="divquestion11"]/ul/li[2]/a', '//*[@id="divquestion11"]/ul/li[3]/a')).click() #第十二题 driver.find_element_by_xpath(selection('//*[@id="divquestion12"]/ul/li[1]/a', '//*[@id="divquestion12"]/ul/li[2]/a', '//*[@id="divquestion12"]/ul/li[3]/a')).click() js="var q=document.documentElement.scrollTop=2400" driver.execute_script(js) #第十三题 driver.find_element_by_xpath(selection('//*[@id="divquestion13"]/ul/li[1]/a', '//*[@id="divquestion13"]/ul/li[2]/a', '//*[@id="divquestion13"]/ul/li[3]/a', '//*[@id="divquestion13"]/ul/li[3]/a')).click() #第十四题 driver.find_element_by_xpath(selection('//*[@id="divquestion14"]/ul/li[1]/a', '//*[@id="divquestion14"]/ul/li[2]/a', '//*[@id="divquestion14"]/ul/li[3]/a', '//*[@id="divquestion14"]/ul/li[4]/a')).click() driver.find_element_by_xpath(selection('//*[@id="divquestion14"]/ul/li[5]/a', '//*[@id="divquestion14"]/ul/li[6]/a', '//*[@id="divquestion14"]/ul/li[7]/a')).click() #第十五题 driver.find_element_by_xpath(selection('//*[@id="divquestion15"]/ul/li[1]/a', '//*[@id="divquestion15"]/ul/li[2]/a', '//*[@id="divquestion15"]/ul/li[3]/a')).click() js="var q=document.documentElement.scrollTop=3200" driver.execute_script(js) #第十六题 driver.find_element_by_xpath('//*[@id="q16"]').send_keys('我爱她') #第十七题 driver.find_element_by_xpath('//*[@id="q17"]').send_keys('165+') #第十八题 driver.find_element_by_xpath('//*[@id="q18"]').send_keys('0') #第十九题 driver.find_element_by_xpath('//*[@id="q19"]').send_keys(hours) #第二十题 driver.find_element_by_xpath(selection('//*[@id="divquestion20"]/ul/li[1]/a', '//*[@id="divquestion20"]/ul/li[2]/a', '//*[@id="divquestion20"]/ul/li[3]/a')).click() #提交按钮 driver.find_element_by_xpath(selection('//*[@id="submit_button"]')).click() print('第'+str(i)+'次填写成功') driver.quit()time_write(100)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
下好第三方包,代码复制粘贴运行即可展示效果
作者:Atom_QQ2022313691
链接:https://atom2022313691.blog.csdn.net/article/details/106838604
来源:CSDN
著作权归作者所有,转载请联系作者获得授权,切勿私自转载。