#!/usr/bin/env python3
"""
调试发布页面结构
"""
import asyncio
from browser.browser import BrowserManager
async def debug_publish_page():
"""调试发布页面"""
print("="*60)
print("调试发布页面结构")
print("="*60)
browser = BrowserManager(headless=False)
await browser.start()
try:
page = await browser.new_page()
print("\n1. 导航到发布页面...")
await page.goto("https://creator.xiaohongshu.com/publish/publish?source=official", timeout=60000)
await page.wait_for_load_state("load", timeout=60000)
await asyncio.sleep(3)
print(" ✓ 页面已加载")
print("\n2. 检查页面结构...")
# 检查是否有遮挡元素
page_info = await page.evaluate("""
() => {
// 查找所有可能的遮挡元素
const overlays = [];
// 检查常见的遮挡元素
const selectors = [
'.d-popover',
'.modal',
'.dialog',
'[class*="mask"]',
'[class*="overlay"]',
'[class*="guide"]',
'[class*="tutorial"]'
];
selectors.forEach(sel => {
const elements = document.querySelectorAll(sel);
elements.forEach(el => {
const style = window.getComputedStyle(el);
if (style.display !== 'none' && style.visibility !== 'hidden') {
overlays.push({
selector: sel,
className: el.className,
zIndex: style.zIndex
});
}
});
});
// 查找 TAB 元素
const tabs = Array.from(document.querySelectorAll('div.creator-tab')).map(tab => ({
text: tab.textContent.trim(),
className: tab.className,
visible: tab.offsetParent !== null
}));
// 查找上传内容区域
const uploadContent = document.querySelector('div.upload-content');
return {
url: window.location.href,
overlays: overlays,
tabs: tabs,
hasUploadContent: !!uploadContent,
uploadContentClass: uploadContent ? uploadContent.className : null
};
}
""")
print(f"\n URL: {page_info['url']}")
print(f" 有上传内容区域: {page_info['hasUploadContent']}")
if page_info['overlays']:
print(f"\n ⚠️ 发现 {len(page_info['overlays'])} 个遮挡元素:")
for overlay in page_info['overlays']:
print(f" - {overlay['selector']}: z-index={overlay['zIndex']}")
else:
print("\n ✓ 没有发现遮挡元素")
print(f"\n 找到 {len(page_info['tabs'])} 个 TAB:")
for tab in page_info['tabs']:
print(f" - '{tab['text']}' (visible: {tab['visible']})")
# 尝试移除所有遮挡
print("\n3. 尝试移除遮挡...")
removed = await page.evaluate("""
() => {
const removed = [];
// 移除所有可能的遮挡元素
const selectors = [
'.d-popover',
'.modal',
'.dialog',
'[class*="mask"]',
'[class*="overlay"]',
'[class*="guide"]',
'[class*="tutorial"]'
];
selectors.forEach(sel => {
const elements = document.querySelectorAll(sel);
elements.forEach(el => {
const style = window.getComputedStyle(el);
if (style.display !== 'none' && style.visibility !== 'hidden') {
el.remove();
removed.push(sel);
}
});
});
return removed;
}
""")
if removed:
print(f" ✓ 移除了 {len(removed)} 个遮挡元素")
else:
print(" 没有需要移除的遮挡元素")
# 再次检查 TAB
print("\n4. 检查 TAB 是否可点击...")
tab_status = await page.evaluate("""
() => {
const tabs = document.querySelectorAll('div.creator-tab');
const results = [];
tabs.forEach(tab => {
const text = tab.textContent.trim();
const rect = tab.getBoundingClientRect();
const x = rect.left + rect.width / 2;
const y = rect.top + rect.height / 2;
const target = document.elementFromPoint(x, y);
const isBlocked = !(target === tab || tab.contains(target));
results.push({
text: text,
isBlocked: isBlocked,
targetElement: target ? target.tagName + '.' + target.className : 'null'
});
});
return results;
}
""")
for status in tab_status:
blocked_str = "❌ 被遮挡" if status['isBlocked'] else "✅ 可点击"
print(f" '{status['text']}': {blocked_str}")
if status['isBlocked']:
print(f" 遮挡元素: {status['targetElement']}")
# 尝试点击"上传图文"
print("\n5. 尝试点击'上传图文' TAB...")
try:
# 方法1: 直接点击
tab = await page.query_selector('div.creator-tab:has-text("上传图文")')
if tab:
await tab.click()
print(" ✓ 方法1成功: 直接点击")
else:
print(" ✗ 方法1失败: 未找到元素")
except Exception as e:
print(f" ✗ 方法1失败: {e}")
# 方法2: 使用 JavaScript 点击
try:
await page.evaluate("""
() => {
const tabs = document.querySelectorAll('div.creator-tab');
for (const tab of tabs) {
if (tab.textContent.trim() === '上传图文') {
tab.click();
return true;
}
}
return false;
}
""")
print(" ✓ 方法2成功: JavaScript 点击")
except Exception as e2:
print(f" ✗ 方法2失败: {e2}")
print("\n6. 等待观察...")
print(" 浏览器窗口将保持打开,按 Ctrl+C 退出")
await asyncio.sleep(300)
except KeyboardInterrupt:
print("\n\n用户中断")
except Exception as e:
print(f"\n✗ 错误: {e}")
import traceback
traceback.print_exc()
finally:
await browser.close()
print("\n" + "="*60)
if __name__ == "__main__":
asyncio.run(debug_publish_page())