基于网页版QQ,批量发送QQ消息


import time
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.support.ui import Select

init_page = "http://web2.qq.com/"

content = '测试内容'
def OpenChrome():
    global init_page,content

    friends_number = 0
    option = webdriver.ChromeOptions()
    option.add_argument('disable-infobars')

    driver = webdriver.Chrome(chrome_options=option)
    driver.maximize_window()
    driver.get(init_page)
    driver.implicitly_wait(30)
    driver.find_element_by_id('contact').click()
    driver.implicitly_wait(3)

    group_list = driver.find_elements_by_xpath("//ul[@id='friend_groupList']/li")
    print('当前好友分组有'+str(len(group_list))+'个')
    
    for z in range(len(group_list)):
        group_title_id = 'groupTitle-'+str(z)
        current_group = driver.find_element_by_id(group_title_id)
        print(group_title_id)
        current_group.click()
        group_title_text = driver.find_element_by_xpath("//div[@id='"+group_title_id+"']/span").text

        group_body_id = 'groupBodyUl-'+str(z)
        group_members = driver.find_elements_by_xpath("//ul[@id='"+group_body_id+"']/li")
        print('当前分组为:"'+group_title_text+'",有'+str(len(group_members))+'位好友')

        # 由于加载的时候可能会有部分元素莫名加载不出,无法选择和定位
        # for x in range(len(group_members)):
        #     # 由于会有元素不在屏幕内,不能点击的情况,所以取消这种方法
        #     # group_members[x].click()

        #     # 获取元素的ID
        #     current_friend_id = group_members[x].get_attribute('id')
        #     friends_name = driver.find_element_by_xpath("//li[@id='"+current_friend_id+"']/p").text
        #     chat_text = driver.find_element_by_id('chat_textarea')
        #     chat_text.send_keys(content)
        #     # driver.find_element_by_id('send_chat_btn').click()
        #     driver.find_element_by_id('panelRightButton-5').click()
        #     print(friends_name)
        #     # 由于元素可能不在加载的范围内,所以也不能定位
        #     # driver.execute_script("arguments[0].scrollIntoView();", group_members[x+1])
        #     pass
        # 模糊匹配全部好友元素的情况
        all_friends = driver.find_elements_by_xpath("//li[contains(@id, 'friend-item-friend-')]")
        print('好友数量['+str(len(all_friends))+']')
        current_group.click()
        friends_number+=len(group_members)
        pass
    print('当前总共有['+str(friends_number)+']位好友')
OpenChrome()
Last modification:February 28th, 2021 at 03:37 pm
硬币投入口