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).
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.
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
orxdotool
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.
Infos
- xautomation man page: https://linux.die.net/man/7/xautomation
- xdotool man page: https://man.cx/xdotool
- wmctrl man page: https://linux.die.net/man/1/wmctrl
- Selenium: https://en.wikipedia.org/wiki/Selenium_(software)
- Selenium downloads: https://www.selenium.dev/downloads/
« Previous 1 2 3
Buy this article as PDF
(incl. VAT)