from multiprocessing import Event
from selenium import webdriver
from selenium.webdriver.support.events import (
    AbstractEventListener,
    EventFiringWebDriver,
)
import time

PATH = "driver/geckodriver.exe"
URL = "https://selenium.dunossauro.live/exercicio_07.html"


class listener(AbstractEventListener):
    def before_click(self, element, webdriver):
        # o valor element ja retorna o elemento passado pelo trigger, nao precisa procurar denovo
        print(element.text)
        # text_before = webdriver.find_element_by_id(element).text
        # print(f"Before click {text_before}")

    def after_click(self, element, webdriver):
        print(element.text)
        # text_after = webdriver.find_element_by_id(element).text
        # print(f"After click {text_after}")


try:
    browser = webdriver.Firefox(executable_path=PATH)
    trigger = EventFiringWebDriver(browser, listener())
    print("Success")

except Exception as e:
    print(e)


trigger.get(URL)
time.sleep(3)

#  lsita com os valores a procurar dentro do DOM
click_list = ("nome", "email", "senha")

for element in click_list:
    #  printa o elemento sendo clicado
    print(f"Clicking on {element}")
    trigger.find_element_by_id(f"l{element}").click()

    #  envia para o input o valor do elemento (por teste só)
    trigger.find_element_by_id(f"{element}").send_keys(f"Teste{element}@{element}")
    time.sleep(0.5)

#  no final, clica no botao, XPATH É VIDA TIME
trigger.find_element_by_xpath('//*[@id="btn"]').click()
