import json
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
# Path to your exported cookies.json
COOKIES_FILE = "cookies_for_selenium.json"
# URL = "https://polybuzz.ai"
URL = "https://www.polybuzz.ai/character/chat/free-use-farm-KwmD2?chatScene=1"
# Start Firefox
options = webdriver.FirefoxOptions()
options.add_argument("--start-maximized")
driver = webdriver.Firefox(options=options)
# Step 1: Load the page once so the domain is set
driver.get(URL)
# Step 2: Load cookies from file
with open(COOKIES_FILE, "r", encoding="utf-8") as f:
cookies = json.load(f)
for cookie in cookies:
if "sameSite" in cookie:
cookie.pop("sameSite")
if "expiry" in cookie:
cookie["expiry"] = int(cookie["expiry"])
try:
driver.add_cookie(cookie)
except Exception as e:
print("Skipping cookie:", cookie.get("name"), e)
# Step 3: Refresh to apply cookies
driver.refresh()
print("Cookies applied, you should be logged in.")
# Example loop: click button + scrape new messages
seen = set()
def extract_new_messages():
ps = driver.find_elements(By.CSS_SELECTOR, ".msg-list .messagebox p")
new_msgs = []
for p in ps:
text = p.text.strip()
if text and text not in seen:
seen.add(text)
new_msgs.append(text)
return new_msgs
try:
while True:
# Click "Go on" button if present
try:
btn = driver.find_element(By.CSS_SELECTOR, ".go-on-botton")
btn.click()
print("Clicked Go on button")
except:
pass
# Extract new messages
msgs = extract_new_messages()
if msgs:
with open("messages.txt", "a", encoding="utf-8") as f:
for m in msgs:
f.write(m + "\n")
print("Saved:", msgs)
time.sleep(30)
except KeyboardInterrupt:
driver.quit()