import time

from selenium.common import TimeoutException

from base.WebKeys import WebKeys


class CreateChapterPage(WebKeys):
    def create_Chapter(self):
        # 显性等待作者专区元素并进行点击
        locator = ("link text", "作家专区")
        self.locator_with_wait(*locator).click()
        #切换windows窗口
        self.change_window(-1)
        # 点击章节管理
        locator = ("partial link text", "章节管理")
        self.locator_with_wait(*locator).click()
        # 切换窗口句柄
        self.change_window(-1)

        try:
            # 如果有数据，点击某个元素
            locator = ("xpath", "//*[@id='hasContentDiv']/div[1]/div/a")
            self.locator_with_wait(*locator).click()
        except TimeoutException:
            # 如果没有数据，点击另一个元素
            locator = ("xpath", "//*[@id='noContentDiv']/div/a")
            self.locator_with_wait(*locator).click()
        except Exception:
            print("章节管理元素未找到")

        # 显性等待章节名元素出现并进行输入从操作
        locator = ("id", "bookIndex")
        bookname=self.locator_with_wait(*locator).send_keys(f"章节名-{str(int(time.time()))}")

        # 显性等待章节内容元素出现并进行输入从操作
        locator = ("id", "bookContent")
        self.locator_with_wait(*locator).send_keys(f"章节内容-{str(int(time.time()))}")

        # 显性等待收费元素出现并点击
        locator = ("css selector", '[name="isVip"][value="1"]')
        self.locator_with_wait(*locator).click()

        # 显性等待提交元素出现并进行点击
        locator = ("id", "btnRegister")
        self.locator_with_wait(*locator).click()
        return bookname

