需求:将1.txt里面的每一行内容作为输入项,实文生图或者文生视频
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
import time
browser = webdriver.Edge()
print(type(browser))
expected_url = "https://klingai.kuaishou.com/text-to-image/new"
browser.maximize_window()
browser.get("https://klingai.kuaishou.com/")
WebDriverWait(browser, 1000).until(lambda driver: driver.current_url == expected_url)
print("URL has changed to the expected URL!")
with open('C:\\Users\\zwx1293889\\Desktop\\1.txt', 'r', encoding='utf-8') as file:
lines = file.readlines()
for line in lines:
try:
WebDriverWait(browser, 60).until(EC.presence_of_element_located((By.XPATH, '/html/body/div[1]/div[1]/div/main/div[1]/div[4]/div[1]/div[2]/div[1]/div/textarea')))
input_element = browser.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/main/div[1]/div[4]/div[1]/div[2]/div[1]/div/textarea')
input_element.send_keys(Keys.CONTROL + "a")
input_element.send_keys(Keys.DELETE)
input_element.send_keys(line.strip())
time.sleep(5) # 等待5秒
except NoSuchElementException:
print("Input element not found, skipping...")
try:
WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.XPATH, '/html/body/div[1]/div[1]/div/main/div[1]/div[5]/div[2]/div[1]/button')))
button = browser.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/main/div[1]/div[5]/div[2]/div[1]/button')
button.click()
time.sleep(5) # 等待5秒
except NoSuchElementException:
print("Button not found, skipping...")
print("All lines have been inputted.")
time.sleep(2000)
星火认知——科大讯飞 | 通义千问 ——阿里 |
---|---|
混元——腾讯 | 盘古——华为 |
日日新——商汤科技 | 曹植——达观数据 |
– | – |
孟子——澜舟科技 |
文生图
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
import time
import pandas as pd
class WebAutomation:
def __init__(self):
self.browser = webdriver.Edge()
print(type(self.browser))
def navigate_and_wait(self, url):
self.browser.maximize_window()
self.browser.get(url)
WebDriverWait(self.browser, 1000).until(lambda driver: driver.current_url == url)
print("URL has changed to the expected URL!")
def process_lines(self, file_path):
# 使用pandas读取Excel文件
df = pd.read_excel(file_path)
print(df.head()) # 显示DataFrame的前几行
print(df.columns) # 显示所有列名
# 假设正确的列名为'YourColumnName',请根据实际情况修改
lines = df['prompt(中)'][1:] # 从第二行开始读取数据
for line in lines:
try:
WebDriverWait(self.browser, 60).until(
EC.presence_of_element_located((By.XPATH, '/html/body/div[1]/div[1]/div/main/div[1]/div[4]/div[1]/div[2]/div[1]/div/textarea'))
)
input_element = self.browser.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/main/div[1]/div[4]/div[1]/div[2]/div[1]/div/textarea')
input_element.send_keys(Keys.CONTROL + "a")
input_element.send_keys(Keys.DELETE)
input_element.send_keys(str(line).strip())
time.sleep(3) # 等待3秒
except NoSuchElementException:
print("Input element not found, skipping...")
try:
WebDriverWait(self.browser, 10).until(
EC.presence_of_element_located((By.XPATH, '/html/body/div[1]/div[1]/div/main/div[1]/div[5]/div[2]/div[1]/button'))
)
button = self.browser.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/main/div[1]/div[5]/div[2]/div[1]/button')
button.click()
time.sleep(3) # 等待3秒
except NoSuchElementException:
print("Button not found, skipping...")
print("All lines have been inputted.")
self.cookies = self.browser.get_cookies()
# 打印cookies
for cookie in self.cookies:
print(cookie)
if __name__ == "__main__":
automation = WebAutomation()
automation.navigate_and_wait("https://klingai.kuaishou.com/")
# 请确保这里使用的是正确的Excel文件路径和列名
automation.process_lines('C:\\Users\\zwx1293889\\Desktop\\可灵剩余.xlsx')
time.sleep(2000)