How To Track Dynamically Updating Code Using Selenium In Python
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.
# 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')
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
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module