{"id":43813,"date":"2014-10-20T07:00:00","date_gmt":"2014-10-20T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2014\/10\/20\/scripting-an-internet-explorer-window\/"},"modified":"2014-10-20T07:00:00","modified_gmt":"2014-10-20T07:00:00","slug":"scripting-an-internet-explorer-window","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20141020-00\/?p=43813","title":{"rendered":"Scripting an Internet Explorer window"},"content":{"rendered":"<p>\nToday&#8217;s Little Program takes a random walk through MSDN\nby starting at the <code>Create&shy;Process<\/code> page\nand randomly clicking links.\nThe exercise is not as important as the technique it demonstrates.\n<\/p>\n<pre>\nfunction randomwalk(ie, steps) {\n for (var count = 0; count &lt; steps; count++) {\n  WScript.StdOut.WriteLine(ie.document.title);\n  var links = ie.document.querySelectorAll(\"#mainSection a\");\n  do {\n   var randomLink = links[Math.floor(Math.random() * links.length)];\n  } while (randomLink.protocol != \"http:\");\n  WScript.StdOut.WriteLine(\"Clicking on \" + randomLink.innerText);\n  randomLink.click();\n  while (ie.busy) WScript.Sleep(100);\n }\n}\n<\/pre>\n<p>\n(I&#8217;m assuming the reader can figure out what language this script is\nwritten in.\nIf you have to ask, then you probably won&#8217;t understand this article\nat all.\nI am also not concerned with random number bias because Little Program.)\n<\/p>\n<p>\nTo talk a random walk through MSDN,\nwe ask for all the links in the\n<code>main&shy;Section<\/code> element.\nNote that I&#8217;m taking an undocumented dependency on the structure\nof MSDN pages.\nThis structure has changed in the past,\nso <i>be aware that the script may stop working at any time\nif the MSDN folks choose to reorganize their pages<\/i>.\nI&#8217;m not too worried since this is a demonstration,\nnot production code.\nIn real life, you are probably going to script a Web page that\nyour team designed (as part of automated testing),\nso taking a dependency on the DOM is something the QA team\ncan negotiate with the development team.\n(If your real life scenario really is walking through the MSDN\ncontent, then you should use the\n<a HREF=\"http:\/\/services.msdn.microsoft.com\/ContentServices\/ContentService.asmx\">\nMSDN content API<\/a>.\n<a HREF=\"http:\/\/msdn.microsoft.com\/en-us\/magazine\/cc163541.aspx\">\nHere&#8217;s sample code<\/a>.)\n<\/p>\n<p>\nAnyway, we grab a link at random,\nbut throw away anything that is not an http: link.\nThis avoids us accidentally navigating into a mailto: link,\nfor example.\n<\/p>\n<p>\nWe then invoke the <code>click()<\/code> method on the link to\nsimulate the user clicking on it.\nWe could also have just navigated to\n<code>randomLink.href<\/code>,\nbut I&#8217;m using the <code>click()<\/code> method\nbecause it is more general.\nYour script may want to tick some checkboxes and then click\nthe Submit button,\nand those actions can&#8217;t be performed by navigation.\n<\/p>\n<p>\nWe then wait for the Web page to settle down.\nI&#8217;m lazy and am simply using a polling loop.\nIf you want to be clever,\nyou could listen on the\n<code>on&shy;ready&shy;state&shy;change<\/code> event,\nbut this is just a Little Program, so I&#8217;m content to just poll.\n<\/p>\n<p>\nOnce we have settled on the new page,\nwe loop back and do it again.\n<\/p>\n<p>\nNow we just need to drive this helper function.\n<\/p>\n<pre>\nvar ie = new ActiveXObject(\"InternetExplorer.Application\");\nie.visible = true;\nie.navigate(\"http:\/\/msdn.microsoft.com\/ms682425\");\n\/\/ Wait for it to load\nwhile (ie.busy) WScript.Sleep(100);\nrandomwalk(ie, 10);\nie.Quit();\n<\/pre>\n<p>\nWe create our own instance of Internet Explorer\n<a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2009\/12\/02\/9931183.aspx\">\nso we can change its carpet\nwithout getting anybody upset<\/a>,\nnavigate it to the\n<code>Create&shy;Process<\/code> page,\nand wait for the page to load.\nWe then use our <code>random&shy;walk<\/code> function to\nclick on ten successive links,\nand then when we&#8217;re done, we\n<a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2011\/11\/18\/10238335.aspx\">\nbring in the demolition crew to destroy the browser we created<\/a>.\n<\/p>\n<p>\nFor extra evil, you could commandeer an existing Internet Explorer\nwindow\nrather than creating your own.\n(Now you&#8217;re barging into somebody&#8217;s house and rearranging the furniture.)\n<\/p>\n<pre>\nvar shellWindows = new ActiveXObject(\"Shell.Application\").Windows();\nfor (var i = 0; i &lt; shellWindows.Count; i++) {\n var w = shellWindows.Item(i);\n if (w.name == &quot;Windows Internet Explorer&quot;) {\n  randomwalk(w, 10);\n  break;\n }\n}\n<\/pre>\n<p>\nMaking the appropriate changes to <code>random&shy;walk<\/code> so\nas not to be MSDN-specific is left as an exercise.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today&#8217;s Little Program takes a random walk through MSDN by starting at the Create&shy;Process page and randomly clicking links. The exercise is not as important as the technique it demonstrates. function randomwalk(ie, steps) { for (var count = 0; count &lt; steps; count++) { WScript.StdOut.WriteLine(ie.document.title); var links = ie.document.querySelectorAll(&#8220;#mainSection a&#8221;); do { var randomLink = [&hellip;]<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-43813","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Today&#8217;s Little Program takes a random walk through MSDN by starting at the Create&shy;Process page and randomly clicking links. The exercise is not as important as the technique it demonstrates. function randomwalk(ie, steps) { for (var count = 0; count &lt; steps; count++) { WScript.StdOut.WriteLine(ie.document.title); var links = ie.document.querySelectorAll(&#8220;#mainSection a&#8221;); do { var randomLink = [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/43813","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=43813"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/43813\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=43813"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=43813"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=43813"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}