准备材料:
1.python
2.selenium-3.141.0-py2.py3-none-any
python install selenium文件路径
3.geckodriver(用以调用firefox浏览器 谷歌/IE自行搜索)
将3、4文件都放在python.exe所在的同级目录
4.firefox浏览器 也可使用IE/chrome 但需要在3中使用相应驱动支持
5.脚本文件
# coding = utf-8
from selenium import webdriver
import time
from selenium.webdriver import ActionChains
username = str(input("请输入qq账号"))
password = str(input("请输入qq密码"))
people = str(input("请输入对方的qq号码"))
message=str(input("请输入发送内容"))
myLoop=int(input("请输入重复发送次数"))
browser = webdriver.Firefox()
browser.get("https://i.qq.com/")
time.sleep(2)
browser.switch_to.frame('login_frame')
js2 = "document.getElementById('switcher_plogin').click()"
browser.execute_script(js2)
userBox=browser.find_element_by_id("u")
pwBox=browser.find_element_by_id("p")
userBox.clear()
pwBox.clear()
browser.find_element_by_id("u").clear()
userBox.send_keys(username)
pwBox.send_keys(password)
time.sleep(2)
js2 = "document.getElementById('login_button').click()"
browser.execute_script(js2)
time.sleep(5)
browser.switch_to.frame('tcaptcha_iframe')
source = browser.find_element_by_id('tcaptcha_drag_button')
isLogin=0
base=172
while(isLogin==0):
try:
browser.find_element_by_id('tcaptcha_drag_button')
ActionChains(browser).drag_and_drop_by_offset(source, base, 0).perform()
base+=1
time.sleep(2)
if(base==180):
base=172
except:
isLogin = 1
isSubmit=0
print("登陆成功 系统检测中....等待20s")
time.sleep(10)
browser.switch_to.default_content()
browser.find_element_by_xpath("//a[@id='aIcenter']").click()
for i in range(myLoop):
browser.find_element_by_xpath("//div[@id = '$1_substitutor_content']").click()
js2 = 'document.getElementsByClassName("set-audience-drop")[0].setAttribute("class", "set-audience-drop"); '
browser.execute_script(js2)
browser.find_element_by_xpath("//a[@data-hottag = 'moodpost.secret.part']").click()
time.sleep(2)
js2 = 'document.getElementsByClassName("fSelector_search")[0].value ='+people
browser.execute_script(js2)
browser.find_element_by_xpath("//li[@title='QQ:"+people+"']").click()
browser.find_element_by_xpath("//a[@data-hottag='moodpost.secret.select_friends']").click()
js2 = 'document.getElementById("$1_content_content").innerHTML="'+message+'"'
browser.execute_script(js2)
js2 = 'document.getElementsByClassName("btn-post gb_bt")[0].click()'
browser.execute_script(js2)
print("---提交")
time.sleep(2)
print('第'+str(i)+'条发送完毕 '+str(i)+'/'+str(myLoop))
browser.close();
也可以配合windows任务计划程序实现定时发送
Selenium:css_selector定位详解(css selector和xpath的比较)
Selenium Webdriver教程