I am trying to query a chatbot using selenium in Python to input queries and then run a jQuery to get the response.
It works nicely, assuming that the chatbot's webpage loads fast enough. If for whatever reason the webpage is slow to respond, then my script queries for a response and either gets a blank or gets the previous response.
How can I solve this without resorting to huge sleep delays? At the moment the page might take up to 5-7 seconds between replies or it may take only 1-2 seconds.
Is there a way to instruct the program to wait for the server's response before running the jQuery?
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.firefox.options import Options
import re
from requests import get
class AIBot:
def __init__(self):
# Initialize selenium options
self.opts = Options()
self.opts.add_argument("-headless")
self.browser = webdriver.Firefox(options=self.opts)
self.url = "http://demo.vhost.pandorabots.com/pandora/talk?botid=b0dafd24ee35a477"
def get_form(self):
# Find the form tag to enter your message
self.browser.implicitly_wait(10.0)
self.elem = self.browser.find_element_by_name('input')
def send_input(self, userInput):
# Submits your message
fOne = '<\/?[a-z]+>|<DOCTYPE'
fTwo = '/<[^>]+>/g'
while True:
try:
self.elem.send_keys(userInput + Keys.RETURN)
except BrokenPipeError:
continue
break
def get_response(self):
# Retrieves response message
jFetch = get("http://code.jquery.com/jquery-1.11.3.min.js").content.decode('utf8')
self.browser.execute_script(jFetch)
response = self.browser.execute_script("""
var main_str = $('font:has(b:contains("Chomsky:"))').contents().has( "br" ).last().text().trim();
main_str = main_str.replace(/Chomsky:/gi,'').replace(/Wikipedia is a great online encyclopedia./gi,
'Wikipedia is your friend. Use it.').replace(/^\\s*[\\r\\n]/gm, '');
return main_str;
""")
return response
def mainLoop():
# Reset variables and connect to AI bot
humanString = None
robotString = None
pb = AIBot()
pb.browser.get(pb.url)
while True:
# User input
humanString = input("Human says: ")
pb.get_form()
if humanString == 'exit':
break
# Bot response
pb.send_input(humanString)
robotString = pb.get_response()
print("Robot says: " + robotString)
pb.browser.close()
if __name__ == '__main__':
mainLoop()
Edit:
Just to clarify, in this particular instance waiting until element is located is not helpful as this webpage just doesn't have any useful elements to target, particularly in the bot's response area.
Maybe there is a way to check if the response variable is the same as previously when the jQuery runs and keep waiting until it changes?