使用selenium处理某手小店自动回复
from tkinter import Tk, Label, Entry, Buttonimport time
from threading import Thread
import pygame
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
from selenium.common.exceptions import TimeoutException
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import random
# 设置浏览器的用户数据目录
chrome_options = Options()
chrome_options.add_argument(
"--user-data-dir=C:\\Users\\Administrator\\AppData\\Local\\Google\\Chrome\\User Data\\Default")
# 初始化 Chrome 浏览器
# 创建全局变量 driver
driver = webdriver.Chrome(options=chrome_options)
reply_texts = ["你好 亲", "在的呢亲", "亲亲在的呢", "亲有什么可以帮您的亲", "亲 我在呢", "好的亲",
"您的问题正在帮您查看", "多云仓随机发货的亲", "不支持指定快递呢亲", "质量不错呢亲"]
def wait_for_inputs():
while not entry1.get() or not entry2.get():
root.update_idletasks()# 更新界面
# 用于等待的函数
def wait_for_element(driver0, xpath, timeout=30):
try:
element = WebDriverWait(driver, timeout).until(
ec.presence_of_element_located((By.XPATH, xpath))
)
return element
except TimeoutException:
return None
def open_chrome():
wait_for_inputs()# 等待用户输入
driver.get("https://im.kwaixiaodian.com/workbench#ud=864662150")# 请将此处的网址替换为你要跳转的实际网址
# time.sleep(40)
def run_code():
"""
执行具体的操作
"""
current_cycle = 0# 初始化当前循环次数为 0
for _ in range(int(entry2.get())):
# 在循环中使用
current_cycle += 1# 在每次循环开始前增加循环次数
root.title(f"快手监控私信 - 第 {current_cycle} 次循环")# 更新标题
if current_cycle == int(entry2.get()):
root.title("当前循环结束")
try:
# 登录成功后的页面直接查询等待是否有新的信息出现
element = wait_for_element(driver,
"/html/body/div/div/div/div/section/main/div/div/div/div/div/div/div/div"
"/div/div/div/div/div/div/div")
if element:# 如果出现新的信息
# 元素找到,执行操作
value = element.text
print("元素的值:", value)
# 在窗口上显示信息
print("指定目标出现,开始查询下个特定目标")
# 下面这个是找三分钟内后面的数字
second_xpath = '/html/body/div/div/div/div/section/main/div/div/div/div/div/div/div/div/div/div/div/div/div/div/div'
second_element = wait_for_element(driver, second_xpath)
value = second_element.text
print("元素的值:", value)
if value == "(0)":
print("暂时没有新的消息")
else:
print("出现新的会话,执行回复操作")
actions = ActionChains(driver)
actions.move_to_element(second_element).perform()# 移动到元素位置
initial_location = second_element.location
# 计算向下移动 5 个单位后的新坐标
new_y = initial_location['y'] - 50
new_location = {'x': initial_location['x'], 'y': new_y}
print("修改后坐标:", new_location)
# 使用新坐标进行移动
actions.move_to_element_with_offset(second_element, new_location['x'], new_location['y']).perform()
print("初始坐标:", initial_location)
print("移动后的坐标:", new_location)
second1_xpath = ('/html/body/div/div/div/div/section/main/div/div/div/div/div/div/div/div'
'/div/div/div/div/div/div/div/div/div/div/div/div/div/div/div['
'2]/div/span')
# 等待倒计时元素可见
wait = WebDriverWait(driver, 10)# 设置最长等待时间为 10 秒
countdown_element = wait.until(
EC.visibility_of_element_located((By.CSS_SELECTOR, "span.SessionBaseCard-topHeadTime")))
# 获取倒计时元素的文本内容
countdown_text = countdown_element.text
# 打印倒计时文本
print(countdown_text)
actions.move_to_element(countdown_element).perform()# 移动到元素位置
new1_location = countdown_element.location
print("新会话坐标:", new1_location)
actions.click().perform()# 点击
time.sleep(1)
target_xpath = ('/html/body/div/div/div/div/section/main/div/div/div/div/div/div/div/div'
'/div/div/div/div/div/div')
target_element = wait_for_element(driver, target_xpath)
value = target_element.text
print("元素的值:", value)
if target_element:
actions = ActionChains(driver)
actions.move_to_element(target_element).perform()# 移动到元素位置
time.sleep(1)
actions.click().perform()# 点击
# 随机选择一条回复文本
random_reply = random.choice(reply_texts)
actions.send_keys(random_reply).perform()
time.sleep(2)
# 等待指定元素出现
wait = WebDriverWait(driver, 10)# 设置最长等待时间为 10 秒
button_element = wait.until(EC.presence_of_element_located((By.XPATH,
'/html/body/div/div['
'1]/div/div/section/main/div/div'
'/div/div/div/div/div/div/div['
'2]/div/div/div/div['
'2]/button')))
# 点击元素
button_element.click()
# # 等待用户输入的间隔时间
time.sleep(int(entry1.get()))
else:
# 元素未找到,处理情况
print("暂时没有新的消息,跳过")
tk_label.config(text="暂时没有新的消息,跳过")
tk_label.after(5000, lambda: tk_label.config(text=""))
except NoSuchElementException as e:
print(f"找不到元素:{e}")
def thread_runner():
"""
在新线程中运行 run_code 函数
"""
Thread(target=run_code).start()
root = Tk()
root.title("快手自动回复")
root.geometry("250x110")# 设置窗口大小为 300x100
tk_label = Label(root, text="结果展示", width=30, bd=10, bg="yellow")
tk_label.pack(pady=3)
# 创建第一个输入框和其上方的文字标签
label1 = Label(root, text="间隔时间:")
label1.pack()
label1.place(x=0, y=50)
entry1 = Entry(root, width=8)
entry1.pack()
entry1.place(x=60, y=50)
# 创建第二个输入框和其上方的文字标签
label2 = Label(root, text="循环次数:")
label2.pack()
label2.place(x=0, y=80)
entry2 = Entry(root, width=8)
entry2.pack()
entry2.place(x=60, y=80)
# 设置按钮的函数
def set_buttons():
button2 = Button(root, text="登录", width=5, command=open_chrome)
button2.pack(pady=10)
button2.place(x=135, y=60)
button = Button(root, text="运行", width=5, command=thread_runner)
button.pack(pady=10)
button.place(x=195, y=60)
set_buttons()
root.mainloop()
DrissionPage 这个框架操作浏览器模拟点击数据抓取很方便,超好用,可以试试,最近工作使用很爽 学习了,最近正在学习selenium。。 大神真多,开眼了 学习了,谢谢 学习一下 感谢分享,学习了 感谢分享,学会了 谢谢大佬啊················ 推荐加上AI搞个资料库会更6