QQ_Zone.py 1.8 KB
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By
from time import sleep
import win32gui
import win32con


driver = webdriver.Firefox()
driver.maximize_window()

driver.get("https://qzone.qq.com/")

# 切换到frame
driver.switch_to.frame("login_frame")

try:
    # 隐式等待
    driver.implicitly_wait(5)
    # 切换到账号密码登录
    driver.find_element_by_id("switcher_plogin").click()
    # 登录
    driver.find_element_by_id("u").send_keys("774355873")
    driver.find_element_by_name("p").send_keys("newlife19110228.")
    driver.find_element_by_id("login_button").click()

    # 退出frame,返回主文档
    driver.switch_to.default_content()
except NoSuchElementException as msg:
    print(msg)

# 图片上传,显式等待
WebDriverWait(driver,10,0.5).until(EC.presence_of_element_located((By.CLASS_NAME,"pic"))).click()
driver.find_element_by_id("qz_app_imageReader_1").click()
sleep(5)
dialog = win32gui.FindWindow(0, u'打开')  # 对话框
ComboBoxEx32 = win32gui.FindWindowEx(dialog, 0, 'ComboBoxEx32', None)
ComboBox = win32gui.FindWindowEx(ComboBoxEx32, 0, 'ComboBox', None)
Edit = win32gui.FindWindowEx(ComboBox, 0, 'Edit', None)  # 输入相对地址
button = win32gui.FindWindowEx(dialog, 0, 'Button', None)  # 确定按钮
win32gui.SendMessage(Edit, win32con.WM_SETTEXT, None, r"E:\Python+Selenium\sc4\cat.jpg")
win32gui.SendMessage(dialog, win32con.WM_COMMAND, 1, button)
driver.find_element_by_id("$1_content_content").send_keys("这是Selenium自动化发布的颤颤和抖抖照片")
sleep(5)  # 等待图片上传到图床
# 发表
driver.find_element_by_link_text("发表").click()