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:
selenium.open chrome url google.com
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
selenium.click search selenium.click 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:
♥url variable.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.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
classoridselectors. In the script above you can replace bothxpathselectors withclassones:
selenium.gettext search iUh30 by class result ♥urland
selenium.click search LC20lb by classThey should even work in Google searches other than in English.
Next lessons will show how emails can be handled by the robot.