[Python] Selenium + Python how to listen for change of element

Discussão em 'Python' iniciado por Stack, Fevereiro 24, 2025.

  1. Stack

    Stack Membro Participativo

    The idea is to create a bot to read message from a chat, and all message are in a ul>li (not neccesary to write message), e.g

    <ul class="message-list">
    <li class="message">
    Hello There
    </li>
    <li class="message">
    Hello
    </li>
    </ul>


    I found expected_conditions from Selenium, to handle if element exist/found, but it's always true, because there is a message but I don't want to continue the code if a message was already in chat, that's not the main idea. And after that, I found EventFiringWebDriver, AbstractEventListener and nothing.

    from selenium import webdriver as wb
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support.events import EventFiringWebDriver, AbstractEventListener
    from selenium.webdriver.common.keys import Keys


    class MyListener(AbstractEventListener):
    def before_navigate_to(self, url, driver):
    print("Before navigate to %s" % url)

    def after_navigate_to(self, url, driver):
    print("After navigate to %s" % url)


    driver = wb.Chrome('C:\python\selenium\chromedriver.exe')
    wait = WebDriverWait(driver, 300)
    # ef_driver = EventFiringWebDriver(driver, MyListener())
    driver.implicitly_wait(15)
    driver.get('https://socket.io/demos/chat/')
    driver.switch_to.frame(driver.find_element_by_class_name('iframe-class'))
    InputName = driver.find_element_by_xpath('/html/body/ul/li[2]/div/input')
    InputName.send_keys('MyNameIsJeff')
    InputName.send_keys(Keys.ENTER)

    Continue reading...

Compartilhe esta Página