{"id":4635,"date":"2014-03-20T14:52:18","date_gmt":"2014-03-20T18:52:18","guid":{"rendered":"http:\/\/www.webperformance.com\/load-testing-tools\/blog\/?page_id=4635"},"modified":"2014-10-13T16:46:26","modified_gmt":"2014-10-13T20:46:26","slug":"scripted-example-clicking-elements","status":"publish","type":"page","link":"https:\/\/www.webperformance.com\/load-testing-tools\/blog\/articles\/real-browser-manual\/building-a-testcase\/scripting-support\/execute-script\/scripted-example-clicking-elements\/","title":{"rendered":"Script example: Clicking a list of elements"},"content":{"rendered":"<p>This step clicks a series of elements by executing a series of XPath lookups. If there were 3 elements to click, their XPaths would\u00a0 be:<\/p>\n<pre><code>\/\/*[@id=\"surveyQuestions\"]\/table\/tbody\/tr[2]\/td[2]\/span\r\n\/\/*[@id=\"surveyQuestions\"]\/table\/tbody\/tr[3]\/td[2]\/span<\/code>\r\n<code>\/\/*[@id=\"surveyQuestions\"]\/table\/tbody\/tr[4]\/td[2]\/span<\/code><\/pre>\n<p>There should be at least one element found, so it returns an error if none are found. It uses findElements() instead of findElement() because findElements() does not throw an exception if nothing is found. This makes it easy to catch all other exceptions and report them errors. If it finds more than one element with a single query, then something is wrong with the XPath, so it also reports this as an error.<\/p>\n<pre><code>function execute(context)\r\n\u00a0\u00a0\u00a0 {\r\n\u00a0\u00a0\u00a0 var index = 2;\r\n\u00a0\u00a0 \u00a0var elements = null;\r\n\u00a0\u00a0\u00a0 var num_found = 0;\r\n\u00a0\u00a0\u00a0 while (true)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 try\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 var xpath = '\/\/*[@id=\"surveyQuestions\"]\/table\/tbody\/tr[' + index + ']\/td[2]\/span';\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 context.logMessage('looking for ' + xpath);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 elements = context.getDriver().findElements(By.xpath(xpath));\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0if (elements.size() === 0)\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0break;\u00a0 \/\/ no more to click\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0if (elements.size() &gt; 1)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return \"Found \" + elements.size() + \" elements using XPath: \" + xpath;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 num_found++;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 elements.get(0).click();\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 catch (error)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0return error.toString();\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 index++;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0 if (num_found === 0)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return \"None found!\";\r\n\u00a0\u00a0\u00a0 return null;\r\n\u00a0\u00a0\u00a0 }\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This step clicks a series of elements by executing a series of XPath lookups. If there were 3 elements to click, their XPaths would\u00a0 be:<br \/>\n\/\/*[@id=&#8221;surveyQuestions&#8221;]\/table\/tbody\/tr[2]\/td[2]\/span<br \/>\n\/\/*[@id=&#8221;surveyQuestions&#8221;]\/table\/tbody\/tr[3]\/td[2]\/span<br \/>\n\/\/*[@id=&#8221;surveyQuestions&#8221;]\/table\/tbody\/tr[4]\/td[2]\/span<br \/>\nThere should be at least one element found, so it returns an error if none are found. It uses findElements() instead of findElement() because findElements() does not throw an exception if nothing is found. This makes it easy to catch all other exceptions and report them errors. If it finds more than one element with a single query, then something is wrong with the XPath, so it also reports this as an error.<br \/>\nfunction execute(context)<br \/>\n\u00a0\u00a0\u00a0 {<br \/>\n\u00a0\u00a0\u00a0 var &hellip; <a href=\"https:\/\/www.webperformance.com\/load-testing-tools\/blog\/articles\/real-browser-manual\/building-a-testcase\/scripting-support\/execute-script\/scripted-example-clicking-elements\/\">Continue reading &raquo;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"parent":4720,"menu_order":2,"comment_status":"open","ping_status":"open","template":"manual-page.php","meta":{"footnotes":""},"class_list":["post-4635","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.webperformance.com\/load-testing-tools\/blog\/wp-json\/wp\/v2\/pages\/4635","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.webperformance.com\/load-testing-tools\/blog\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.webperformance.com\/load-testing-tools\/blog\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.webperformance.com\/load-testing-tools\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.webperformance.com\/load-testing-tools\/blog\/wp-json\/wp\/v2\/comments?post=4635"}],"version-history":[{"count":9,"href":"https:\/\/www.webperformance.com\/load-testing-tools\/blog\/wp-json\/wp\/v2\/pages\/4635\/revisions"}],"predecessor-version":[{"id":4764,"href":"https:\/\/www.webperformance.com\/load-testing-tools\/blog\/wp-json\/wp\/v2\/pages\/4635\/revisions\/4764"}],"up":[{"embeddable":true,"href":"https:\/\/www.webperformance.com\/load-testing-tools\/blog\/wp-json\/wp\/v2\/pages\/4720"}],"wp:attachment":[{"href":"https:\/\/www.webperformance.com\/load-testing-tools\/blog\/wp-json\/wp\/v2\/media?parent=4635"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}