I am trying to extract the content from divs
on a web page using Selenium.
The web page is dynamically generated and every second or so there is a new div inserted into the HTML on the web page.
So far I have the following code:
from selenium import webdriver
chrome_path = r"C:\scrape\chromedriver.exe"
driver = webdriver.Chrome(chrome_path)
driver.get("https://website.com/")
messages = []
for message in driver.find_elements_by_class_name('div_i_am_targeting'):
messages.append(message.text)
for x in messages:
print(x)
Which works fine, the problem is it only prints the values of the divs
on the page at the time it is run, I want to continuously extract the text from the_div_i_am_targeting
and there are new divs
appearing on the page every second or so.
I found this: Handling dynamic div's in selenium Which was the closest related question I could find, but it isn't a match for my question and there are no answers.
How can I update the above code so that it continuously prints the contents of the divs on the page for my chosen div (in this example div_i_am_targeting
) including new divs that are added to the page after the program runtime?
some text
* this pattern is repeated indefinitely on the page, so there are many of the same divs generated. The code above works fine, but I need to find a way to get the program to continue to run and continuously capture the new divs as they are created.Thanks for the suggestion about iterating over the loop with messate.text not in messages. – Gary Nov 24 '18 at 13:17