Reputation: 12608
I am new to using Python. I am trying to write a script using Selenium that opens a Firefox browser and checks to see if a specific phrase is in the page source.
So far I have this...
from selenium import webdriver
driver = webdriver.Firefox()
driver.get('http://www.example.com/')
current_page_source = driver.page_source
print(current_page_source)
if "my phrase" in current_page_source :
print ("Phrase Is Present")
else:
print ("Phrase Is Not Present")
This is working great, but I would like this to continually check the source every 30 seconds.
Do I need a to use a for loop to achieve this?
Upvotes: 1
Views: 148
Reputation: 2913
In order to run that code every 30 seconds, you need two things:
You need to create a 30-second delay. The easiest way is to use the time
module to create a delay. For example:
import time
# some code
time.sleep(30) # delays for 30 seconds
You need to repeat the action. The easiest way is to use a while loop that checks a condition. For example:
keep_running = True
while(keep_running):
# your repeatable code
That way you can later set some logic in the code inside the while-loop that may set the keep_running
variable to False, thus ending the loop (for example, once it's found the text you're looking for 1000 times, or if you press a button, etc).
Upvotes: 1