2

How do I track dynamically updating code on a website?

On a website there is a part of the code that shows notifications. This code gets updates frequently, and I would like to use selenium to capture the changes.

Example:

# Setting up the driver
from selenium import webdriver
EXE_PATH = r'C:/Users/mrx/Downloads/chromedriver.exe'
driver = webdriver.Chrome(executable_path=EXE_PATH)

# Navigating to website and element of interest
driver.get('https://whateverwebsite.com/')
element = driver.find_element_by_id('changing-element')

# Printing source at time 1
element.get_attribute('innerHTML')

# Printing source at time 2
element.get_attribute('innerHTML')

The code returned for time 1 and time 2 is different. I could of cause capture this using some time of loop.

# While loop capturing changes
results=list()
while True:
    print("New source")
    source=element.get_attribute('innerHTML')
    new_source=element.get_attribute('innerHTML')
    results.append(source)
    while source==new_source:
        time.sleep(1)

Is there a smarter way to do this using selenium's event listener? new_source=element.get_attribute('innerHTML')

1 Answer 1

1

Try wait by selenium way with WebDriverWait, selenium provide a method .text_to_be_present_in_element, you can try following approach.

First you need following import:

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

Try the bellow code:

element = driver.find_element_by_id('changing-element')

# Printing source at time 1
element.get_attribute('innerHTML')

#something that makes the element change

WebDriverWait(driver, 10).until(expected_conditions.text_to_be_present_in_element((By.ID, 'changing-element'), 'expected_value'))

# Printing source at time 2
element.get_attribute('innerHTML')

But if it isn't found, it will return an TimeoutException error, please handle with try/except

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. It looks similar to my approach. I will try it out later today, and get back to you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.