from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from bs4 import BeautifulSoup
import time


class Browser:
    def __init__(self, headless=False):
        options = webdriver.ChromeOptions()
        if headless:
            options.add_argument("--headless")
        self.driver = webdriver.Chrome(
            service=Service(ChromeDriverManager().install()), options=options
        )

    def open(self, url):
        self.driver.get(url)

    def click(self, selector, by=By.CSS_SELECTOR):
        element = self.driver.find_element(by, selector)
        element.click()

    def type(self, selector, text, by=By.CSS_SELECTOR):
        element = self.driver.find_element(by, selector)
        element.clear()
        element.send_keys(text)

    def get_page_source(self):
        return self.driver.page_source

    def parse_html(self):
        return BeautifulSoup(self.driver.page_source, "lxml")

    def close(self):
        self.driver.quit()
