For Advanced

Lesson 3/10


You learned how to find webpage elements and perform actions on them with the selenium. commands, so it’s time to see some of these functions in combat.

Run this script: chrome url
selenium.type g1ant search q by name
delay 2
selenium.presskey enter search btnK by name
selenium.gettext search //*[@id="rso"]/div[1]/div/div[1]/div/div/div[1]/a[1]/div/cite by xpath result ♥url
selenium.newtab ♥url
delay 2
selenium.activatetab google by title
selenium.setattribute name value value ‴robotic process automation‴ search q by name
selenium.presskey enter search q by name search search //*[@id="rso"]/div[3]/div/div[1]/div/div/div[1]/a/h3 by xpath

Let’s translate this code into the natural language:

  1. Open Chrome and go to
  2. Enter “g1ant” into the element named “q” (which is the search phrase input box).
  3. Wait 2 seconds.
  4. Press Enter key on the element named “btnK” (Google Search button).
  5. Get the text — the target URL address — from the first search result (based on its XPath) and store it in the ♥url variable.
  6. Open a new tab and navigate to this address.
  7. Wait 2 seconds.
  8. Activate the tab, which has “google” in its title (that is, the first tab).
  9. Since the search phrase input box is already filled with “g1ant”, you can’t use the selenium.type command again to enter a new search phrase (whatever you insert with the selenium.type command, it will be attached to the existing text instead of replacing it). But you can change the value of the value attribute of the search phrase input box element, which stores the phrase that was just searched for, using the selenium.setattribute command. When you set the value attribute to “robotic process automation”, this phrase will be inserted into the input box.
  10. With a new search phrase in the input box, press Enter key in this box to start a new search.
  11. Click the first search result (selected by its XPath).

Note: This script assumes your Google search is in English. In other languages it might not work properly.

Also, XPaths are prone to changes: if any of nodes in the XPath tree is altered or moved, it will affect the whole element's address, so sometimes it's better to use class or id selectors. In the script above you can replace both xpath selectors with class ones:

selenium.gettext search iUh30 by class result ♥url

and search LC20lb by class

They should even work in Google searches other than in English.

Next lessons will show how emails can be handled by the robot.