Automate your web logins

Selenium Searches

From the earlier park booking example, you saw that xte and xautomation had some limitations when a variable list of options was presented. Luckily Selenium has a number of functions that can be used for searching HTML tags and text. Like the last example, the first step is to open the web page and inspect the structure manually (Figure 6).

Figure 6: Getting list details.

For this example, the Inspector shows that each status entry in the list has a <div class="jss97"> that could have a Past, Available, Not Busy, or Full status. The top-level <div class="jss94"> has both the times and the status messages. Knowing the top-level div class now makes it possible to search for the park's time slots and get the status of each of the times.

Figure 7 shows an example that searches for the first Not Busy time slot. As in the earlier xdotool example, the time slot list needs to be clicked to open. In Python code, this is done by finding and then clicking on the timefield object.

Figure 7: Finding the first Not Busy time.

The key piece in this code is:

thetimes = find_elements_by_class_name("jss94")

This operation will build an array (thetimes) of all the time slots with their status messages.

Next, a for loop can examine each time slot. In this example, the code looks for the first time a time slot is Not Busy:

# Get the top level div
thetimes = browser.find_elements_by_class_name("jss94")
for itimes in thetimes:
    # Find the first "Not Busy"
    if "Not Busy" in itimes.text:
        print(itimes.text)
        itimes.click()
        break

Logic could be written for different conditions, like looking for time slots between 9 and 11am, and if none are found, then looking for time slots between 2 and 4pm.

Final Comments

After using the various methods discussed in this article, I found that:

  • Often my apps written during off hours would not work during peak times because I had not accounted for the increased peak callup delays.
  • The browser search dialog with xte/xdotool was extremely useful because it allowed me to jump to specific areas of a web page, rather than tabbing to it.
  • Creating apps with xte or xdotool is considerably easier than using Python with Selenium. I found that some web pages were incredibly complex, and it often took some time to find the required IDs that Selenium needed.
  • For large web entry pages, you can always create automated web logins by mixing and matching the xte/xdotool utilities and Python.
  • Two huge advantages in using Selenium are being able to add some decision-making logic and implicity_wait() methods, which wait until the page is ready and is a lot more efficient than putting in a long sleep time.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Perl: Web Regression Test

    Testing complex web applications does not necessarily mean investing in expensive, proprietary tools such as Test Director or Silk Performer. Selenium is for free; it can remotely control any major browser, and it is programmable in Perl.

  • Workspace: Text Expander

    A couple of utilities and a dash of Bash scripting are all you need to roll out a simple yet flexible text expander.

  • Tutorial – Devilspie2

    Stop battling your window manager to position things as you like – make scripts do all the hard work!

  • Kitchen Kiosk

    Create a kiosk display from an old eReader to show data culled from Home Automation, Raspberry Pis, and Arduinos.

  • Servile Guardian

    What is making the lights on the router flicker so excitedly? An intruder? We investigate with pfSense on a Protectli micro appliance and a screen scraper to email the information.

comments powered by Disqus

Direct Download

Read full article as PDF:

Price $2.95

News