WebExecute

WebExecute[cmd]

executes the command cmd in a web browser.

WebExecute[{cmd1,cmd2,}]

executes a list of commands in sequence.

WebExecute[session,cmds]

executes cmds in the specified web session.

Details

  • WebExecute executes commands in a web browser through a web driver.
  • If a web session has been started with StartWebSession, WebExecute[cmds] executes commands in that session, as specified by $CurrentWebSession.
  • If no web session has been started, WebExecute[cmds] starts a web session, executes cmds, then ends the web session.
  • WebExecute[cmd] returns either the output of the command or a Success or Failure object.
  • WebExecute[{cmd1,cmd2,}] returns a list of the results for the cmdi.
  • Commands in WebExecute are typically specified in the following forms:
  • "cmd"a command without arguments
    "cmd" arga command with one argument
    "cmd"{arg1,arg2,}a command with multiple arguments
  • WebExecute supports the following commands related to complete webpages:
  • "OpenPage"urlopen the specified webpage
    "PageURL"get the URL of the current webpage
    "PageTitle"get the title of the current webpage
    "PageBack"go to the previous webpage
    "PageForward"go to the next webpage
    "RefreshPage"refresh the current webpage
    "PageHyperlinks"get the list of hyperlinks on the current webpage
    "CapturePage"capture an image of the current webpage
  • WebExecute supports the following commands related to elements of webpages:
  • "LocateElements"elemgive the list of elements on a webpage matching elem
    "ClickElement"elemclick an element on a webpage (e.g. a hyperlink)
    "HoverElement"elemhover over an element on a webpage
    "TypeElement"{elem,"text"}type text into an element on a webpage (e.g. an input field)
    "SubmitElement"elemsubmit a form using the specified element (e.g. a button)
    "ElementSelected"elemwhether an element is selected
    "ElementEnabled"elemwhether an element is enabled
    "ElementTag"elemthe HTML tag for the element
    "ElementText"elemthe text, if any, associated with an element
    "HideElement"elemmake an element invisible
    "ShowElement"elemmake an element visible
  • Elements of webpages can be specified in any of the following forms:
  • WebElementObject[...]a previously located element (e.g. from "LocateElements")
    "HyperlinkText" "text"hyperlinks with the specified text
    "PartialHyperlinkText""form"hyperlinks with text containing "form"
    "Tag""tag"elements with the specified HTML tag
    "XPath"pathelements on the specified XPath
    "CSSSelector"patternelements matching the specified CSS selector pattern
  • WebExecute supports the following commands related to windows:
  • "BrowserWindows"get the WebWindowObject list
    "SetBrowserWindow"windowmake WebWindowObject window active
    "WindowSize"get the current window size as {width,height}
    "SetWindowSize"{w,h}set the current window size
    "WindowPosition"get the position of a window
    "SetWindowPosition"{window,{x, y}}set the screen position of window
    "WindowMaximize"windowmaximize window
    "WindowMinimize"windowminimize window
    "WindowFullscreen"make the current window fullscreen
  • WebExecute allows the following general commands:
  • "JavascriptExecute""code"execute the specified string of JavaScript code
  • WebExecute uses the protocol defined by W3C.

Examples

open all close all

Basic Examples  (5)

Start a web session:

In[1]:=
Click for copyable input
Out[1]=

Open a webpage in the current session:

In[2]:=
Click for copyable input
Out[2]=

Close the session:

In[3]:=
Click for copyable input

Start a web session:

In[1]:=
Click for copyable input
Out[1]=

Open a webpage:

In[2]:=
Click for copyable input
Out[2]=

Get the URL of the page open in the browser:

In[3]:=
Click for copyable input
Out[3]=

Go back to the previous page in the browser tabs history:

In[4]:=
Click for copyable input

Go one page forward in the browser tabs history:

In[5]:=
Click for copyable input

Refresh the page:

In[6]:=
Click for copyable input

Get the title of the page open in the browser:

In[7]:=
Click for copyable input
Out[7]=

Get hyperlinks on the page open in the browser:

In[8]:=
Click for copyable input
Out[8]//Short=

Close the current web session:

In[9]:=
Click for copyable input

Run multiple commands in a single command:

In[1]:=
Click for copyable input
Out[1]=

Start a web session:

In[1]:=
Click for copyable input
Out[1]=

Open a webpage in the current session:

In[2]:=
Click for copyable input
Out[2]=

Locate header elements on the page using "LocateElements":

In[3]:=
Click for copyable input
Out[3]=

Find the element text of the HTML headers found previously:

In[4]:=
Click for copyable input
Out[4]=

Find paragraphs of text on a webpage with "XPath":

In[5]:=
Click for copyable input
Out[5]=

Close the session:

In[6]:=
Click for copyable input

Start a web session:

In[1]:=
Click for copyable input
Out[1]=

Open a webpage in the current session:

In[2]:=
Click for copyable input
Out[2]=

Find the search input box using "LocateElements" with "CSSSelector":

In[3]:=
Click for copyable input
Out[3]=

Use "TypeElement" to type into the search box found previously:

In[4]:=
Click for copyable input

Submit the form with the query:

In[5]:=
Click for copyable input

Check the page URL to see that the query was submitted:

In[6]:=
Click for copyable input
Out[6]=

Close the session:

In[7]:=
Click for copyable input

Scope  (3)

Introduced in 2019
(12.0)