1.先看结构:
声明:我是初学,可能有不合理的地方。
2.Base层。
我是把原来一个kimi的自动问答的代码改过来。
分析:其实我是新手,因为我用的浏览器是固定的,也没有打算和别人用。所以浏览器层面年的全部写死。
其他功能用到什么添什么。一步步完善。
后期我想这个这基层一直用下去。所以会一步步完善的。
base_page.py的内容如下:
import time
from selenium import webdriver
from selenium.webdriver.edge.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import pyperclip
import os
class BasePage:
def __init__(self):
edge_user_data_dir = r"C:\Users\Administrator\AppData\Local\Microsoft\Edge\User Data\Default"
edge_options = Options()
edge_options.use_chromium = True
edge_options.add_argument('--disable-extensions') # 禁用浏览器扩展
edge_options.add_argument('--disable-gpu') # 禁用GPU硬件加速
# edge_options.add_argument('--headless') # 禁用GPU硬件加速
edge_options.add_argument(f"--user-data-dir={edge_user_data_dir}")
self.driver = webdriver.Edge(options=edge_options)
self.driver.maximize_window()
self.wait = WebDriverWait(self.driver, 300)
# self.driver = driver
# self.driver.implicitly_wait(10)
def keep_browser_open(self):
"""保持浏览器窗口打开,等待用户操作."""
input("按回车键退出程序并关闭浏览器...")
def js_condition(self,driver):
"""自定义等待条件函数,检查JavaScript返回值"""
return driver.execute_script("return document.readyState") == "complete"
def open_url(self,url):
self.driver.get(url)
self.wait.until(self.js_condition)
print("页面加载完成")
return True
def find_element(self, loc):
try:
return self.wait.until(EC.element_to_be_clickable(loc))
except Exception as e:
print(f"元素未找到:{loc}")
return False
def find_elements(self, loc):
elements = self.wait.until(EC.presence_of_all_elements_located(loc))
if not elements: # 可选:检查是否找到元素,如果没有,打印提示信息
print(f"没有找到匹配的元素:{loc}")
return elements
def click_element(self,loc):
element = self.find_element(loc)
element.click()
# self.driver.execute_script("arguments[0].click();", element)
def set_text(self,loc,text):
element=self.find_element(loc)
element.send_keys(text)
def full_path(self,filename):
current_dir = os.getcwd()
# 确保文件名加上.txt扩展名
if not filename.endswith('.txt'):
filename += ".txt"
full_path = os.path.join(current_dir, filename)
return full_path
def clipboard_content_to_file(self,filename):
full_path = self.full_path(filename)
clipboard_text = pyperclip.paste()
with open(full_path, 'w', encoding='utf-8') as file:
file.write(clipboard_text)
print(f"剪贴板内容已成功写入到文件: {filename}")
def read_line_from_file(self,filename):
full_path = self.full_path(filename)
numeric_line = None # 初始化为None,表示尚未找到符合条件的行
try:
with open(full_path, 'r', encoding='utf-8') as file:
for line in file:
# 检查行是否以数字开头
if line.strip().startswith(tuple('0123456789')):
numeric_line = line.rstrip('\n') # 找到第一行后移除行尾的换行符并赋值
break # 终止循环
except FileNotFoundError:
print(f"文件 {filename} 未找到。")
except Exception as e:
print(f"读取文件时发生错误: {e}")
print("读取当前行:",numeric_line)
return numeric_line
def del_line_from_file(self, filename):
full_path = self.full_path(filename)
numeric_line = None # 初始化为None,表示尚未找到符合条件的行
lines_to_write_back = [] # 用于存储除了被删除行外的所有行
try:
with open(full_path, 'r', encoding='utf-8') as file:
found = False # 标记是否已找到并处理符合条件的行
for line in file:
if not found and line.strip().startswith(tuple('0123456789')):
numeric_line = line.rstrip('\n') # 找到第一行后移除行尾的换行符并赋值
found = True # 设置标志,表示已找到并处理了符合条件的行
else:
lines_to_write_back.append(line) # 其他行保留,准备写回文件
if found: # 只有在确实找到并处理了符合条件的行后才重写文件
with open(full_path, 'w', encoding='utf-8') as file:
file.writelines(lines_to_write_back)
except FileNotFoundError:
print(f"文件 {filename} 未找到。")
except Exception as e:
print(f"读取或修改文件时发生错误: {e}")
print("删除当前行:", numeric_line)
return numeric_line
def append_content_to_file(self,filename, content):
full_path = self.full_path(filename)
try:
with open(full_path, 'a', encoding='utf-8') as file:
file.write(content + '\n') # 内容后添加换行符,以便于区分多条内容
print(f"内容已成功追加到文件: {filename}")
except Exception as e:
print(f"写入文件时发生错误: {e}")
测试用的代码,都通过了。
url = "https://kimi.moonshot.cn/"
case= BasePage()
case.open_url(url)
#lowImage___hU90c
# img_loc="By.CLASS_NAME", 'login____RTRY'
# img_loc=By.CLASS_NAME, 'lowImage___hU90c'
#
# if case.click_element(img_loc):
# print("ok")
edit_loc= By.XPATH,'//div[@data-slate-node="element"]'
text="你好吗?"
case.send_text(edit_loc,text)
send_loc=By.ID, "send-button"
case.click_element(send_loc)
case.keep_browser_open()
3.page层
分析:因为我计划用于kimi或讯飞或其他,所以在规划时。计划用主域名当成关键字。而每一部分不再分成独立模块,如登录,主页,等。如何有跳转的话,后期根据情况写在base层。
import time
from class_learn.base.base_page import BasePage
from selenium.webdriver.common.by import By
import pyperclip
import os
class KimiPage(BasePage):
def __init__(self):
super().__init__() # 假设BasePage也有初始化driver的逻辑,则需要调用super().__init__()
def web_ready(self,url,ok_loc,nok_loc):
if self.open_url(url):
if self.find_element(ok_loc):
print("发现头像,登录成功")
else:
self.click_element(nok_loc)
def new_page(self,new_loc):
self.click_element(new_loc)
time.sleep(2)
def get_questions(self, op_loc, op_loc1, edit_loc, keywords, send_loc, copy_loc):
self.click_element(op_loc)
time.sleep(1)
self.click_element(op_loc1)
time.sleep(1)
self.set_text(edit_loc, keywords)
self.click_element(send_loc)
time.sleep(2)
self.click_element(copy_loc)
def set_text_and_send(self,edit_loc,text,send_loc):
self.click_element(edit_loc)
time.sleep(1)
self.set_text(edit_loc,text)
self.click_element(send_loc)
def click_copy_and_save(self,copy_loc):
pass
case = KimiPage()
url = "https://kimi.moonshot.cn/"
nok_loc=By.CLASS_NAME, 'login____RTRY' #未登录
ok_loc=By.CLASS_NAME, 'lowImage___hU90c' #已登录
case.web_ready(url,ok_loc,nok_loc)
new_loc=By.XPATH,"//div[@data-testid='msh-sidebar-new']"
case.new_page(new_loc)
op_loc=By.CSS_SELECTOR,".icon___zTPKp svg"
op_loc1=By.CSS_SELECTOR,".itemContainer___eYZxh .content___EPfWU"
op_loc2=By.CSS_SELECTOR,"div:nth-child(2) > .itemContainer___eYZxh .content___EPfWU"
edit_loc=By.XPATH,'//div[@data-slate-node="element"]'
keywords="泌尿系统"
send_loc=By.ID, "send-button"
copy_loc=By.XPATH, "//span[contains(.,'复制')]"
# case.get_questions(op_loc,op_loc1,edit_loc,keywords,send_loc,copy_loc)
# case.clipboard_content_to_file(keywords)
file=keywords+"_ques"
for i in range(100):
if i%10==0:
case.new_page(new_loc)
time.sleep(2)
ask=case.read_line_from_file(keywords)
case.get_questions(op_loc,op_loc2,edit_loc,ask,send_loc,copy_loc)
case.del_line_from_file(keywords)
case.append_content_to_file(file, f"第{i}章 {ask}")
case.append_content_to_file(file,pyperclip.paste())
case.keep_browser_open()
基本完成了,可以自动生成100个问题,自动回答,自动追加到文本中,自动删除已经回答过的问题。方便系统错误后,接着进行,每10个问题自动开始一个新的页面。
感觉比原来好用多了。清晰了,看来代码要不停的写才可以。