For this example we'll be creating a simple Google auto-searcher that grabs the total number of results for a query & the time take to execute the query.
First, we'll create a function that takes in a search query and prints the number of results found + time taken to execute the query:
from simpleseleniumwrapper import WebDriver as SimpleSelenium #Imports Simple Selenium
def getNumberOfSearchResultsForQuery(query):
#... Code for this example will go here
Now inside the function let's initiate the Simple Selenium Webdriver:
Looking at the search bar for google.com, I notice that the tag is a "textarea", which is a fairly uncommon element on most webpages. Let's try using the textarea to locate the search bar element:
search_bar=driver.by_tag("textarea") #Grabs the first instance of <textarea>
Now we need Simple Selenium to type in the search query. Luckily Simple Selenium has the .write function, which will automatically type text like a human:
search_bar.write(query) #Writes the query into the search bar with human-like keystrokes
After typing, we need to hit the enter key:
driver.actionChains.press_key("enter").perform() #Use action chains to hit enter key
Once Simple Selenium hits the enter key, it's time to grab the number of results returned text from the results page.
Looking at the page, the results returned text has an ID "result-stats" that we can use to grab the text element:
Let's use that ID to locate the number of results found text on the page:
number_of_search_results=driver.by_id("result-stats").text() #Grabs the "About x results" text
The last step is dissecting the text & grabbing valuable information. We can use Python's .split function to do this:
#Split number of results into results number + time taken to execute query
total_number_results_clean=number_of_search_results.split("About ")[1].split(" ")[0]
time_taken_for_query=number_of_search_results.split("(")[1].split(" ")[0]
Now let's print the number of results found & time taken for the query:
#Prints data with formatted strings
print(f"Found {total_number_results_clean} results for \"{query}\" on google.com")
print(f"Query took {time_taken_for_query} seconds")
And finally, close the Webdriver cleanly:
driver.close() #Close driver cleanly
The entire function code looks like this:
def getNumberOfSearchResultsForQuery(query):
#Init Simple Selenium driver
driver=SimpleSelenium("chrome",maximized=True,save_profile=True,profile_name="awesome sauce")
driver.visit("https://www.google.com/") #Visit google.com
search_bar=driver.by_tag("textarea") #Grabs the first instance of <textarea>
search_bar.write(query) #Search for cool math games with human-like keystrokes
driver.actionChains.press_key("enter").perform() #Use action chains to hit enter key
number_of_search_results=driver.by_id("result-stats").text() #Grabs the "About x results" text
#Split number of results into results number + time taken to execute query
total_number_results_clean=number_of_search_results.split("About ")[1].split(" ")[0]
time_taken_for_query=number_of_search_results.split("(")[1].split(" ")[0]
print(f"Found {total_number_results_clean} results for \"{query}\" on google.com")
print(f"Query took {time_taken_for_query} seconds")
driver.close() #Close driver cleanly
Let's test the function out:
getNumberOfSearchResultsForQuery("Cool math games") #Prints number of results & time taken for the query "Cool math games"
As you can see from the pictures above, the function works! Hopefully this example helps inspire you to build your own projects using Simple Selenium.