Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Selenium Webdriver to get periodically updated content

As an example, the chat site Omegle always displays on its homepage the current number of users online, which I am able to extract with this python script using the headless HTMLUnit Webdriver in Selenium:

from selenium import webdriver    
driver = webdriver.Remote(desired_capabilities=webdriver.DesiredCapabilities.HTMLUNITWITHJS)
driver.get('http://www.omegle.com/')
element = driver.find_element_by_id("onlinecount")
print element.text.split()[0]

The output is like:

22,183

This number is dynamically generated and updated periodically by a script, and I want to read just this dynamically updated content at intervals without repeatedly loading the entire page with driver.get. What Selenium Webdriver method or functionality will let me do that?

This article seems like a relevant lead, though it led me nowehere.

like image 411
Abhranil Das Avatar asked Jan 14 '13 20:01

Abhranil Das


1 Answers

This is untested, but I think the following might work:

from selenium import webdriver    
from time import sleep
driver = webdriver.Remote(desired_capabilities=webdriver.DesiredCapabilities.HTMLUNITWITHJS)
driver.get('http://www.omegle.com/')
interval = 10  #or whatever interval you want
while True:
    element = driver.find_element_by_id("onlinecount")
    print element.text.split()[0]
    sleep(interval)

I think if you find the element after it's been altered, it will give you the new value.

like image 140
munk Avatar answered Oct 19 '22 21:10

munk