【文生视频】绕过登录实现页面自动化(可灵)

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
需求:将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)

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值