|
| 1 | +(* ::Package:: *) |
| 2 | + |
1 | 3 | BeginPackage["WebTools`", {"CloudObject`", "CURLLink`"}]; |
2 | 4 |
|
3 | 5 | wtInstallWebTools::usage = "wtInstallWebTools[] launches the default web driver which allows Mathematica to communicate with a web browser. wtInstallWebTools[driver] launches the specified driver."; |
|
42 | 44 | wtTagName::usage = ""; |
43 | 45 | wtXPath::usage = ""; |
44 | 46 |
|
| 47 | +wtSelector::usage=""; |
| 48 | + |
45 | 49 | $wtWebDriver::usage = ""; |
46 | 50 | $wtWebDriverBaseURL::usage = ""; |
47 | 51 | (* Javascript based functions *) |
48 | 52 |
|
49 | | -wtGetPageHtml::usage = ""; |
| 53 | +wtGetPageHtml::usage = "Get HTML of current tab."; |
| 54 | + |
| 55 | +wtGetHtml::usage = "Get part of HTML of current tab, search method include wtId[], wtXPath and wtSelector[]. |
| 56 | +Option \"Seletion\" could be set to \"inner\" or \"outer\" which control the returned part of the selected HTML segment."; |
| 57 | + |
| 58 | +wtGetPageURL::usage = "Get URL of current tab."; |
| 59 | + |
| 60 | +wtOffAlert::usage = "wtOffAlert[] would turn off all bump up windows including alerts(), confirm() and prompt() and automatically confirm them." |
50 | 61 |
|
51 | 62 | Begin["`Private`"]; |
52 | 63 |
|
|
76 | 87 | wtCaptureWebPage[x___] := screenshot[x]; |
77 | 88 |
|
78 | 89 | $wtSupportedWebDrivers = Switch[ $SystemID , |
79 | | - "Windows-x86-64", {"ChromeDriver","InternetExplorerDriver","MicrosoftWebDriver"}, |
| 90 | + "Windows-x86-64", {"ChromeDriver","InternetExplorerDriver","MicrosoftWebDriver","FirefoxDriver"}, |
80 | 91 | "MacOSX-x86-64", {"ChromeDriver"}, |
81 | 92 | _, {} |
82 | 93 | ]; |
|
89 | 100 | "ChromeDriver", {"Chrome","http://localhost:9515"}, |
90 | 101 | "InternetExplorerDriver", {"InternetExplorer","http://localhost:5555"}, |
91 | 102 | "MicrosoftWebDriver", {"Edge", "http://localhost:17556"}, |
| 103 | + "FirefoxDriver", {"Firefox", "http://localhost:4444"}, |
92 | 104 | _, Null ]; |
93 | 105 | If[ TimeConstrained[URLRead[$wtWebDriverBaseURL<>"/status"],0.5] === $Aborted, (* only launch driver if not running *) |
94 | 106 | dir = FileNameJoin[{ $WebToolsDirectory, "WebDriver", driver, $SystemID }]; |
|
113 | 125 | ], |
114 | 126 | "FirefoxDriver", |
115 | 127 | Switch[ $SystemID, |
116 | | - "Windows-x86-64", Null, |
| 128 | + "Windows-x86-64", StartProcess[FileNameJoin[{dir,"geckodriver.exe"}]], |
117 | 129 | "MacOSX-x86-64", Null, |
118 | 130 | _, Null |
119 | 131 | ], |
|
134 | 146 |
|
135 | 147 | QueryMethod[ wtElementClassName[_String] ] ^:= "class name"; |
136 | 148 | QueryMethod[ wtCssSelector[_String] ] ^:= "css selector"; |
137 | | -QueryMethod[ Id[_String] ] ^:= "id"; |
138 | | -QueryMethod[ Name[_String] ] ^:= "name"; |
| 149 | +QueryMethod[ wtId[_String] ] ^:= "id"; |
| 150 | +QueryMethod[ wtName[_String] ] ^:= "name"; |
139 | 151 | QueryMethod[ wtLinkText[_String] ] ^:= "link text"; |
140 | 152 | QueryMethod[ wtPartialLinkText[_String] ] ^:= "partial link text"; |
141 | 153 | QueryMethod[ wtTagName[_String] ] ^:= "tag name"; |
|
248 | 260 |
|
249 | 261 | wtGetPageHtml[] := wtJavascriptExecute["return document.getElementsBywtTagName('html')[0].innerHTML;"] |
250 | 262 |
|
251 | | -GetHtmlForId[id_String] := wtJavascriptExecute[ "return document.getElementById('" <> id <> "').innerHTML;"] |
| 263 | +(*Get part of HTML*) |
| 264 | +Options[wtGetHtml]={"Selection"->"outer"}; |
| 265 | + |
| 266 | +wtGetHtml[] := wtGetPageHtml[] |
| 267 | +wtGetHtml[wtSelector[sel_String],OptionsPattern[]] := wtJavascriptExecute[ "return document.querySelector('" <> sel <> "')."<>OptionValue["Selection"]<>"HTML;"] |
| 268 | +wtGetHtml[wtXPath[xp_String],OptionsPattern[]] := wtJavascriptExecute["document.evaluate('"<>xp<>"', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue."<>OptionValue["Selection"]<>"HTML;"] |
| 269 | +wtGetHtml[wtId[id_String],OptionsPattern[]] := wtJavascriptExecute["return document.getElementById('" <> id <> "')."<>OptionValue["Selection"]<>"HTML;"] |
| 270 | + |
| 271 | +(*Get Current URL*) |
| 272 | +wtGetPageURL[]:=wtJavascriptExecute["return window.location.href;"] |
| 273 | + |
| 274 | +(*Turn off all alerts! They are annoying*) |
| 275 | +wtOffAlert[]:=wtJavascriptExecute[ "window.alert=function(){return 1}; window.confirm=function(){return 1}; window.prompt=function(){return 1};"] |
252 | 276 |
|
253 | 277 | End[]; |
254 | 278 |
|
|
0 commit comments