Home > database >  Track changes within DOM element with MutationObserver Python
Track changes within DOM element with MutationObserver Python

Time:11-17

I found a website that pushes darts scores. Each time a new score is published, I would like to be notified.

import time
import selenium
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait

URL = 'https://live.dartsdata.com/'
driver = webdriver.Chrome('/Users/hjam/downloads/chromedriver')
driver.get(URL)

time.sleep(1)
matches = driver.find_elements(By.XPATH, ".//*[@class='sr-match__wrapper srt-base-1 sr-ml-list__match']")
matches[0].click()

I want to retrieve the seconds until the match starts (there are no live matches atm, but idea is the same). I see that this data point is located in

seconds = driver.find_elements(By.XPATH, ".//*[@class='sr-lmt-0-ms-countdown__time srt-primary-7 srm-large']")[-1]

Now I want to use a MutationObserver to track the changes of this element. Each time the element changes, I want it to be printed. Using the example of enter image description here

What am I doing wrong? And is there also a possibility that the output is printed in my python-script?

CodePudding user response:

querySelectorAll is not live. It's returns representations of what the dom was and you're monitoring that snapshot. You need to use getElementsByClassName to hook onto a live element.

  • Related