Intro: Python, splinter.py and Robin

Why would you want to use Splinter? From the docs:

“Splinter is an abstraction layer on top of existing browser automation tools such as Selenium and zope.testbrowser. It has a high-level API that makes it easy to write automated tests of web applications.”

But, you might well ask, "Isn’t it a testing application, and not an RPA tool? "

Some of you may have noticed the overlap between “test automation” and RPA. The objectives are clearly different, but to some extent you can perform test automation with RPA tools, and vice versa. For example, you can do RPA tasks with taiko even though it’s not billed as an RPA tool. We can do at least some ad hoc test automation with Robin.

Splinter isn’t billed as an RPA tool either, as you can see from docs. But we can use it for one.

I may be grossly oversimplifying here, but it appears to me that any test automation tool that has an “evaluate” method can also be used (at least to some extent) for RPA. Splinter’s got that:

https://splinter.readthedocs.io/en/latest/elements-in-the-page.html

Here we’ll give Splinter a test run, with some code shamelessly filched and cloned from the docs.

Screenshot:

Output:

splinter_output

Robin code:

System.GetEnvironmentVariable \
    Name:  'USERNAME' \
    Value=> UserName

set pythonPath to 'C:\Users\\'+UserName+'\AppData\Local\Programs\Python\Python37\python.exe'

set workDir to 'C:\Users\\'+UserName+'\Documents\Robin\9.2\splinter'

set pyScript to workDir +'\sp2.py '

System.RunDOSCommand \
    DOSCommandOrApplication: pythonPath + ' ' + pyScript \
    WorkingDirectory: workDir \
    StandardOutput=> StandardOutput \
    StandardError=> StandardError \
    ExitCode=> ExitCode

Console.Write Message: 'stdout: '+ StandardOutput
Console.Write Message: 'stderr (if any): '+ StandardError
Console.Write Message: 'exit code (if any): ' + ExitCode

Contents of ‘sp2.py’:

from splinter import Browser

with Browser() as browser:
# Visit URL
url = "http://www.google.com"
robindocsurl = "https://robin-language.org/docs/robin-the-rpa-language/"
browser.visit(url)
browser.fill('q', 'robin-language.org')
# Find and click the 'search' button
button = browser.find_by_name('btnK')
# Interact with elements
button.click()
if browser.is_text_present('The RPA Language'):
    print("Yes, the official Robin docs website was found!")
    browser.visit(robindocsurl)
else:
    print("No, it wasn't found... We need to improve our SEO techniques")

Edit: If the Python script above crashes and burns, try this code to find the button:

button = browser.find_by_xpath("//div[@class='FPdoLc tfB0Bf']//input[@name='btnK']")

Or, even better perhaps (since ‘find_by_xpath()’ extracts all elements matching the XPath arg, returning a list of Element objects):

button = browser.find_by_xpath("//div[@class='FPdoLc tfB0Bf']//input[@name='btnK']")[0]

Some other ways to find elements, from the docs:

browser.find_by_css(‘h1’)
browser.find_by_xpath(’//h1’)
browser.find_by_tag(‘h1’)
browser.find_by_name(‘name’)
browser.find_by_text(‘Hello World!’)
browser.find_by_id(‘firstheader’)
browser.find_by_value(‘query’)

Notice we use ‘System.RunDOSCommand’ so we can grab stdout, stderr, and an exit code.

Regards,
burque505

3 Likes