Recently we had a user ask us if it was possible to get Selenium to follow random links on a page.
Selenium can indeed click random links, but it requires some JavaScript tricks on the browser itself:
var browserJavaScript = “var temp = eval_xpath(‘//a\’, window.document);\n” + “temp[Math.floor(Math.random() * temp.length)].innerHTML”;
var linkText = selenium.getEval(browserJavaScript);
selenium.click(“link=” + linkText);
What this does is create a JavaScript string that will run on the
browser itself. The JavaScript is written to retrieve all <a> links on the
page and then randomly select the innerHTML (text) for one of the
links on the page.




Recent Comments