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/div/div/div/div/div/a/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/div/div/div/div/div/a/h3 by xpath
Let’s translate this code into the natural language:
selenium.typecommand again to enter a new search phrase (whatever you insert with the
selenium.typecommand, it will be attached to the existing text instead of replacing it). But you can change the value of the
valueattribute of the search phrase input box element, which stores the phrase that was just searched for, using the
selenium.setattributecommand. When you set the
valueattribute 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
idselectors. In the script above you can replace both
selenium.gettext search iUh30 by class result ♥url
selenium.click 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.