seleniumbase / SeleniumBase

📊 Blazing fast Python framework for web crawling, scraping, testing, and reporting. Supports pytest. Stealth abilities: UC Mode and CDP Mode.
https://seleniumbase.io
MIT License
5.44k stars 983 forks source link

Is there any function to send API requests with uc mode in SeleniumBase? #3169

Closed asibhossen897 closed 1 month ago

asibhossen897 commented 1 month ago

Is there any way to send POST requests or other API requests with uc mode in seleniumbase? Here I would want to use seleniumbase instead of the requests library to send the API requests.

class CFClient:
    def __init__(self, username: str, password: str):
        self.username = username
        self.password = password
        self.session = requests.Session()
        self.console = Console()
        self.driver = Driver(undetected=True, headless=True, browser="chrome")

    def login(self) -> bool:
        self.console.log("Logging in...")
        csrf_token = self.get_csrf("https://codeforces.com/enter")
        r2 = self.session.post(
            "https://codeforces.com/enter",
            data={
                "csrf_token": csrf_token,
                "action": "enter",
                "handleOrEmail": self.username,
                "password": self.password,
            },
            headers={"X-Csrf-Token": csrf_token},
            allow_redirects=True,
        )
        soup2 = BeautifulSoup(r2.text, "html.parser")
        with open("src_code.html", "w", encoding="utf-8") as file:
            file.write(soup2.prettify())

        user_info = soup2.find("div", class_="lang-chooser")
        if not user_info:
            self.console.log("[bold red]ERROR: [/]User info not found.")
            return False

        user_info = user_info.find_all("a")
        if user_info[-1].text.strip() == "Register":
            return False

        self.console.log("Logged in")
        return True

    def get_csrf(self, url) -> str:
        self.driver.get(url)
        soup = BeautifulSoup(self.driver.page_source, "html.parser")
        csrf_element = soup.find("span", class_="csrf-token")
        if csrf_element and "data-csrf" in csrf_element.attrs:
            csrf_token = csrf_element.get("data-csrf")
            print(csrf_token)
            return csrf_token
        self.console.log("[bold red]ERROR: [/]CSRF token not found.")
        return ""
asibhossen897 commented 1 month ago

@mdmintz Please have a look when you can.

mdmintz commented 1 month ago

UC Mode is for stealthy browser actions via the UI only. There's no requests replacement for sending stealthy API requests.