封装了部分接口

This commit is contained in:
jinye_huang 2025-06-02 17:49:46 +08:00
parent 0b10533b10
commit 21aec20273

View File

@ -179,6 +179,90 @@ class DouyinPublisher():
return True
# 新增方法:封装团购选择流程
def select_group_buy(self, product_name, product_info):
try:
# 下拉框选择
tab_selector = self.driver.find_element(By.XPATH, "//div[@class='semi-select select-lJTtRL semi-select-single']")
self.driver.execute_script("arguments[0].click();", tab_selector)
time.sleep(2)
# 选择团购
tab_selector_option = self.driver.find_element(By.XPATH, "//div[@data-code='13010' and @class='select-dropdown-option-video']")
self.driver.execute_script("arguments[0].click();", tab_selector_option)
time.sleep(2)
# 点击产品选择下拉框
product_selector = self.driver.find_element(By.XPATH, "//div[@class='semi-select select-Qm4u8S semi-select-single semi-select-filterable']")
self.driver.execute_script("arguments[0].click();", product_selector)
time.sleep(2)
# 输入产品名称
product_input = self.driver.find_element(By.XPATH, "//input[@class='semi-input semi-input-default' and @placeholder='']")
product_input.send_keys(product_name)
time.sleep(5)
# 选择产品
max_retries = 10
for attempt in range(max_retries):
clicked = self.driver.execute_script("""
let elements = document.querySelectorAll('.semi-select-option');
for (let el of elements) {
if (el.textContent.includes(arguments[0])) {
el.click();
return true;
}
}
return false;
""", product_name)
if clicked:
print(f"成功点击产品选项,尝试次数: {attempt+1}")
break
else:
print(f"等待产品选项出现,尝试次数: {attempt+1}/{max_retries}")
time.sleep(1)
time.sleep(2)
# 输入产品信息
product_info_input = self.driver.find_element(By.XPATH, "//input[@class='semi-input semi-input-default' and @placeholder='如:海底捞超值双人套餐']")
product_info_input.send_keys(product_info)
time.sleep(2)
# 点击确认按钮
confirm_button = self.driver.find_element(By.XPATH, "//div[@class='footer-button-DL8zDh']")
self.driver.execute_script("arguments[0].click();", confirm_button)
time.sleep(2)
return True
except Exception as e:
print(f"选择团购失败: {e}")
return False
# 新增方法:设置定时发布
def set_schedule_time(self, time_str):
try:
# 选择定时发布
schedule_label = self.driver.find_element(By.XPATH, "//label[contains(@class, 'radio-d4zkru')]//span[contains(text(), '定时发布')]/ancestor::label")
self.driver.execute_script("arguments[0].click();", schedule_label)
time.sleep(2)
# 输入时间
time_inputor = self.driver.find_element(By.XPATH, "//input[@class='semi-input semi-input-default' and @type='text' and @placeholder='日期和时间']")
time_inputor.send_keys(time_str)
time.sleep(2)
return True
except Exception as e:
print(f"设置定时发布失败: {e}")
return False
# 新增方法:最后确认接口
def final_confirmation(self, prompt="按任意键继续..."):
input(prompt)
return True
if __name__ == "__main__":
# 使用Safari浏览器
driver = webdriver.Safari()