How can I use a javascript{…} expression as a locator in a Selenium command?
The core documentation states that “All Selenium command parameters can be constructed using both simple variable substitution as well as full javascript”. Since locators are important command parameters you might wonder how you can use Javascript to specify a locator e.g. in a ‘click’ command. The answer is: Used as a locator, the ‘javascript{..}’ idiom has to evaluate to a string that represents a valid Selenium locator, e.g. an identifier, css or xpath locator. So if you have a working locator like id=myButton then javascript{[“id=”,”my”,”Button”].join(”)} should work too. Gotchas: • If your Javascript block contains multiple statements, the value of the last statement determines the value of the block. • You can use the ‘getEval’ command, to debug your Javascript expressions.