|
from playwright.sync_api import sync_playwright
|
|
|
|
|
|
all_request_headers_info = []
|
|
|
|
|
|
def handle_request(request):
|
|
"""
|
|
捕获每个请求的URL, 方法和头部信息
|
|
"""
|
|
|
|
all_request_headers_info.append({
|
|
"url": request.url,
|
|
"method": request.method,
|
|
"headers": request.headers
|
|
})
|
|
|
|
|
|
def main():
|
|
with sync_playwright() as p:
|
|
|
|
|
|
browser = p.chromium.launch(headless=True,
|
|
args=[
|
|
'--no-sandbox',
|
|
'--disable-setuid-sandbox',
|
|
'--disable-dev-shm-usage'
|
|
])
|
|
|
|
|
|
|
|
context = browser.new_context(
|
|
user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:138.0) Gecko/20100101 Firefox/138.0",
|
|
)
|
|
|
|
|
|
page = context.new_page()
|
|
|
|
|
|
|
|
page.on("request", handle_request)
|
|
|
|
print(f"Navigating to https://grok.com/ ...")
|
|
try:
|
|
|
|
page.goto("https://grok.com/", timeout=60000)
|
|
page.wait_for_timeout(5000)
|
|
print("Page loaded. Waiting for 10 seconds for dynamic content or further requests...")
|
|
|
|
|
|
title = page.title()
|
|
print(f"Page title: {title}")
|
|
|
|
if "请稍候…" in page.content() or "Just a moment..." in page.content() or "Cloudflare" in title or "Checking your browser" in title:
|
|
print("Still on a Cloudflare challenge page. Waiting longer or trying interaction...")
|
|
|
|
|
|
try:
|
|
page.wait_for_selector("body:not(:has-text('请稍候…'))", timeout=60000)
|
|
print("Cloudflare challenge likely passed.")
|
|
title = page.title()
|
|
print(f"New page title: {title}")
|
|
page.screenshot(path="cf_passed.png")
|
|
except Exception as e:
|
|
print(f"Failed to pass Cloudflare challenge after extended wait: {e}")
|
|
page.screenshot(path="cf_failed.png")
|
|
else:
|
|
print("Successfully navigated to the page.")
|
|
page.screenshot(path="cf_success.png")
|
|
|
|
page.evaluate("""
|
|
function(){
|
|
const element = document.getElementById('turnstile-widget');
|
|
if (element) {
|
|
element.style.display = 'none';
|
|
}
|
|
}
|
|
""")
|
|
page.wait_for_timeout(10000)
|
|
|
|
try:
|
|
textarea_locator = page.get_by_label("Ask Grok anything")
|
|
textarea_locator.fill("你好")
|
|
print("Successfully entered '你好' into the textarea.")
|
|
except Exception as e:
|
|
print(f"Could not find or fill the textarea with aria-label 'Ask Grok anything'. Error: {e}")
|
|
browser.close()
|
|
return
|
|
|
|
|
|
|
|
try:
|
|
submit_button_locator = page.get_by_role("button", name="Submit")
|
|
submit_button_locator.click()
|
|
print("Successfully clicked the 'Submit' button.")
|
|
except Exception as e:
|
|
print(f"Could not find or click the button with aria-label 'Submit'. Error: {e}")
|
|
browser.close()
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
print("\n--- Cookies ---")
|
|
|
|
cookies = context.cookies()
|
|
if cookies:
|
|
for cookie in cookies:
|
|
print(
|
|
f"Name: {cookie['name']}, Value: {cookie['value']}, Domain: {cookie['domain']}, Path: {cookie['path']}")
|
|
else:
|
|
print("No cookies found.")
|
|
|
|
print("\n--- Request Headers (collected during the session) ---")
|
|
if all_request_headers_info:
|
|
|
|
|
|
for i, req_info in enumerate(all_request_headers_info):
|
|
if req_info['url'] == 'https://grok.com/rest/app-chat/conversations/new':
|
|
datas = {
|
|
'x-xai-request-id': req_info['headers']['x-xai-request-id'],
|
|
'x-statsig-id': req_info['headers']['x-statsig-id'],
|
|
'user-agent': req_info['headers']['user-agent'],
|
|
}
|
|
print(datas)
|
|
return datas
|
|
else:
|
|
print("No requests were intercepted (this is unlikely if the page loaded).")
|
|
|
|
except Exception as e:
|
|
print(f"An error occurred: {e}")
|
|
finally:
|
|
|
|
print("\nClosing browser...")
|
|
page.close()
|
|
browser.close()
|
|
return None
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main() |