Using Selenium in Python to Check for New Content

Viewed 145

I am trying to use Selenium in Python to check if the text has been updated on a page. If the page has been updated it should do some command, if not it should continue to run through the loop checking every 30 seconds for an update. The below is the code I have so far (newbie so go easy on me). The idea is for the code to continually loop through the page looking for a new text added. Not sure if selenium can autorefresh the page or should add code to close the page and start from the beginning.

The code works so far, but can't figure out the loop piece.

from selenium import webdriver
import time

driver = webdriver.Firefox()
driver.get("https://www.somewebsite.com/")

username = driver.find_element_by_id("user_login")
username.clear()
username.send_keys("some username")

password = driver.find_element_by_name("pwd")
password.clear()
password.send_keys("some password")

driver.find_element_by_name("wp-submit").click()
original = driver.find_element_by_css_selector('#feed > div.f-wrap > div:nth-child(1) > div.f-txt')
newer = original
while original == newer:
    body = driver.find_element_by_css_selector('#feed > div.f-wrap > div:nth-child(1) > div.f-txt')
    newer = body.text
    time.sleep(20)


while newer != original:
    print(newer)
1 Answers

Since you wouldn't know what the newer element will be, it's better to check the absence of old element. You may use the below patch to check the same:

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

# create driver object 
driver = webdriver.Firefox()
  
# A URL loads
driver.get("https://www.somewebsite.com/")

# wait 20 seconds before for the element to be invisible
element = WebDriverWait(driver, 20).until(EC.invisibility_of_element_located((By.ID, "myDynamicElement"))
driver.refresh()

Try it out and let me know if it worked!

Related