Skip to content

Commit a427551

Browse files
authored
Merge pull request #13 from wjxway/master
Add new functionality
2 parents 803663c + 132e86b commit a427551

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed
10.3 MB
Binary file not shown.

WebTools/WebDriverAPI.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
setsession["Chrome"] := ($wtCurrentWebSession = post["/session", {"desiredCapabilities" -> {"browserName" -> "chrome"}}, "sessionId"]);
77
setsession["InternetExplorer"] := ($wtCurrentWebSession = post["/session", {"desiredCapabilities" -> {"browserName" -> "internet explorer"}}, "sessionId"]);
88
setsession["Edge"] := ($wtCurrentWebSession = post["/session", {"desiredCapabilities" -> {}, "requiredCapabilities"->{}}, "sessionId"]);
9+
setsession["Firefox"] := ($wtCurrentWebSession = post["/session", {"desiredCapabilities" -> {"browserName" -> "firefox"}}, "sessionId"]);
910
getsession[sessionId_] := get["/session/" <> sessionId];
1011

1112
forward[] := forward[$wtCurrentWebSession];

WebTools/WebTools.m

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
(* ::Package:: *)
2+
13
BeginPackage["WebTools`", {"CloudObject`", "CURLLink`"}];
24

35
wtInstallWebTools::usage = "wtInstallWebTools[] launches the default web driver which allows Mathematica to communicate with a web browser. wtInstallWebTools[driver] launches the specified driver.";
@@ -42,11 +44,20 @@
4244
wtTagName::usage = "";
4345
wtXPath::usage = "";
4446

47+
wtSelector::usage="";
48+
4549
$wtWebDriver::usage = "";
4650
$wtWebDriverBaseURL::usage = "";
4751
(* Javascript based functions *)
4852

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."
5061

5162
Begin["`Private`"];
5263

@@ -76,7 +87,7 @@
7687
wtCaptureWebPage[x___] := screenshot[x];
7788

7889
$wtSupportedWebDrivers = Switch[ $SystemID ,
79-
"Windows-x86-64", {"ChromeDriver","InternetExplorerDriver","MicrosoftWebDriver"},
90+
"Windows-x86-64", {"ChromeDriver","InternetExplorerDriver","MicrosoftWebDriver","FirefoxDriver"},
8091
"MacOSX-x86-64", {"ChromeDriver"},
8192
_, {}
8293
];
@@ -89,6 +100,7 @@
89100
"ChromeDriver", {"Chrome","http://localhost:9515"},
90101
"InternetExplorerDriver", {"InternetExplorer","http://localhost:5555"},
91102
"MicrosoftWebDriver", {"Edge", "http://localhost:17556"},
103+
"FirefoxDriver", {"Firefox", "http://localhost:4444"},
92104
_, Null ];
93105
If[ TimeConstrained[URLRead[$wtWebDriverBaseURL<>"/status"],0.5] === $Aborted, (* only launch driver if not running *)
94106
dir = FileNameJoin[{ $WebToolsDirectory, "WebDriver", driver, $SystemID }];
@@ -113,7 +125,7 @@
113125
],
114126
"FirefoxDriver",
115127
Switch[ $SystemID,
116-
"Windows-x86-64", Null,
128+
"Windows-x86-64", StartProcess[FileNameJoin[{dir,"geckodriver.exe"}]],
117129
"MacOSX-x86-64", Null,
118130
_, Null
119131
],
@@ -134,8 +146,8 @@
134146

135147
QueryMethod[ wtElementClassName[_String] ] ^:= "class name";
136148
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";
139151
QueryMethod[ wtLinkText[_String] ] ^:= "link text";
140152
QueryMethod[ wtPartialLinkText[_String] ] ^:= "partial link text";
141153
QueryMethod[ wtTagName[_String] ] ^:= "tag name";
@@ -248,7 +260,19 @@
248260

249261
wtGetPageHtml[] := wtJavascriptExecute["return document.getElementsBywtTagName('html')[0].innerHTML;"]
250262

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};"]
252276

253277
End[];
254278

0 commit comments

Comments
 (0)