Finding Web Objects Using XPath Expressions

Applies to TestComplete 14.71, last modified on April 22, 2021
Information in this topic applies to web and hybrid mobile applications.

To search for web page elements by their attributes, you can use XPath expressions. In TestComplete tests, you do it by using one of the following methods:

The difference between them is that the EvaluateXPath and FindElements methods return an array of found objects, while the rest of them return the first object that matches the specified XPath. You can find more comparison information on these methods in Find Web Objects.

There can be a situation when properties and attributes of web elements in a web browser differ from those defined in the web page’s source code. For more information on such issues, see Important Notes.

The search expression the methods use must comply with the XPath rules. You can also use XPath standard functions like contains, starts-with and others that may let you create more flexible XPath expressions. You can find detailed information on the XPath syntax at www.w3schools.com/xml/xpath_syntax.

The table below demonstrates some examples of XPath expressions you can use in your tests:

Expression Description
//DIV//IMG All IMG elements that are descendants of any DIV element.
//DIV/* A collection of direct children of any DIV element.
//*[@align='left'] All web page elements that have the align attribute set to left.
//A[starts-with(@href, 'javascript:')] All A elements whose href attribute starts with the javascript: prefix.
//IMG[contains(@src, 'BigMark.png')] All IMG elements whose src attribute contains the BigMark.png substring.
//OL[@class='SimpleOrderedList']/LI[3] All LI elements located at the third position within OL elements whose class attribute is set to SimpleOrderedList.
//TBODY/TR[last()] Last TR elements within any TBODY element.

Tips on Using XPath Expressions

  • To search the entire web page for elements, put a double slash (//) at the beginning of the XPath expression. Otherwise, the specified web page element will be searched for only on the top level of the web page’s hierarchy of elements.

    The following XPath expression matches all IMG elements located anywhere on the page and that have the align attribute set to “center”:

    //IMG[@align='center']

    The expression below matches only those IMG elements that are located on the top hierarchy level of the page:

    IMG[@align='center']

    Since web pages cannot contain IMG elements on the top hierarchy level, the EvaluateXPath method will never find IMG elements when the expression above is passed to the method without a double slash at the beginning.

  • The element and attribute names in search expressions are case-insensitive. However, the sought-for data are case-sensitive. For instance, the following expressions are different from XPath point of view:

    //img[@src = 'Image1.png']
    //img[@src = 'image1.PNG']
    //img[@src = 'IMAGE1.PNG']

    To avoid possible issues, specify values in the case in which they are specified in the source code of the tested web page.

    An alternative approach is to use XPath translate function to convert the data to lower (or upper) case and use lower-case (or upper-case) values. For example:

    //IMG[translate(@src, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') = 'image1.png']

    See the description of the translate function in the XPath specification for complete information about this function and its parameters.

  • When searching for web page elements by their attributes that contain references to files (for instance, the href attribute in A elements, the src attribute in IMG elements, etc.), keep in mind that such references quite often contain relative file paths in the source code of the tested web page. However, when the page is loaded to your Internet browser, such relative paths are replaced with appropriate absolute paths. So, for instance, if you try to search for an IMG element whose src attribute’s value matches the desired file name with a relative path specified in the source code of the page, you will not find the element on the page loaded to the browser.

    In this case, when you do not know the absolute file name specified in the reference, you should use the contains XPath function in your expression to search for elements with absolute file names in references that contain the desired file name. You can find a description of XPath’s contains function in the MSDN Library.

    The XPath expression below matches A elements whose href attribute contains the Orders.html file name:

    //A[contains(@href, 'Orders.html')]

    The following expression searches for A elements with the href attribute equal to the /OrdersApp/Orders.html file name relative to the root of the web site. As a result, such an expression will find no elements with the specified relative file name on the web page loaded to the browser, because all the references on the loaded page will contain only absolute file names:

    //A[@href='/OrdersApp/Orders.html']
  • The hierarchy of elements on a web page loaded to a web browser may slightly differ from the hierarchy that is specified in the page’s source code. Some HTML elements may not be explicitly specified in the source code of the page. However, when the browser loads the page, the hierarchy of web page elements is automatically filled in with such optional elements.

    For instance, when you define a table in the source code of a web page, you can omit the TBODY element within the TABLE element. In this case, the HTML code looks like the following code snippet:

    HTML

    <TABLE id="products">
      <TR>
        <TD>…</TD>
        <TD>…</TD>
      </TR>

      …

      <TR>
        <TD>…</TD>
        <TD>…</TD>
      </TR>
    </TABLE>

    When the page is loaded to the browser, the appropriate hierarchy of web page elements looks like the following:

    HTML

    <TABLE id="products">
      <TBODY>
        <TR>
          <TD>…</TD>
          <TD>…</TD>
        </TR>

        …

        <TR>
          <TD>…</TD>
          <TD>…</TD>
        </TR>
      </TBODY>
    </TABLE>

    In this case, instead of relying on the page’s source code to form the search expression, we recommend that you use a search expression that TestComplete generates for the element automatically. You can view available expressions in the Object Browser or Object Spy.

Examples

FindElement Example

The following example shows how you can use the FindElement method to find web elements by using XPath:

JavaScript, JScript

function Test()
{
  …
  var browser = Sys.Browser();
  var page = browser.Page("*services.smartbear.com/samples/TestComplete*/smartstore/");

  var textbox = page.FindElement("//form/input[@id='instasearch']");
  var searchbutton = page.FindElement("//form/button[contains(@class, 'instasearch-button')]");
  …
}

Python

def Test():
  …
  browser = Sys.Browser()
  page = browser.Page("*services.smartbear.com/samples/TestComplete*/smartstore/")

  textbox = page.FindElement("//form/input[@id='instasearch']")
  searchbutton = page.FindElement("//form/button[contains(@class, 'instasearch-button')]")
  …

VBScript

Sub Test
  …
  Set browser = Sys.Browser()
  Set page = browser.Page("*services.smartbear.com/samples/TestComplete*/smartstore/")

  Set textbox = page.FindElement("//form/input[@id='instasearch']")
  Set searchbutton = page.FindElement("//form/button[contains(@class, 'instasearch-button')]")
  …
End Sub

DelphiScript

procedure Test();
var browser, page, textbox, searchbutton;
begin
  …
  browser := Sys.Browser();
  page := browser.Page('*services.smartbear.com/samples/TestComplete*/smartstore/');

  textbox := page.FindElement('//form/input[@id=''instasearch'']');
  searchbutton := page.FindElement('//form/button[contains(@class, ''instasearch-button'')]');
  …
end;

C++Script, C#Script

function Test()
{
  …
  var browser = Sys["Browser"]();
  var page = browser["Page"]("*services.smartbear.com/samples/TestComplete*/smartstore/");

  var textbox = page["FindElement"]("//form/input[@id='instasearch']");
  var searchbutton = page["FindElement"]("//form/button[contains(@class, 'instasearch-button')]");
  …
}
EvaluateXPath Example

The example below demonstrates how you can use the EvaluateXPath method to get BUTTON web elements whose type attribute is equal to submit. The sample code then simulates a click on the first found element.

Note: The only difference between scripts for web and hybrid mobile applications is in the way you obtain the Page object.

Web

JavaScript

function evalXPath()
{

   // Obtain the Page object
   let url = "http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_button_type";
   Browsers.Item(btIExplorer).Run(url);
   let page = Sys.Browser("*").Page("*");

   // Call the function
   let arr = page.EvaluateXPath("//button[contains(@type, 'submit')]");

   // Check the result
   if (!strictEqual(arr, null))
   {
     // and click the first element that was found
    arr[0].Click(); // Note we refer to the array item
   }
   else 
   {
     // If nothing was found, post a message to the log
     Log.Error("Nothing was found.");
   }
}

JScript

function evalXPath()
{

   // Obtain the Page object
   var url = "http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_button_type";
   Browsers.Item(btIExplorer).Run(url);
   var page = Sys.Browser("*").Page("*");

   // Call the function
   var tmp = page.EvaluateXPath("//button[contains(@type, 'submit')]");

   // Check the result
   if (tmp != null)
   {
     // Convert the array to the JScript-compatible format
     var arr = tmp.toArray();
     // and click the first element that was found
    arr[0].Click(); // Note we refer to the array item
   }
   else 
   {
     // If nothing was found, post a message to the log
     Log.Error("Nothing was found.");
   }
}

Python

def evalXPath():
  # Obtain the Page object
  url = "http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_button_type"
  Browsers.Item[btIExplorer].Run(url)
  page = Sys.Browser('*').Page('*')

  # Call the function 
  arr = page.EvaluateXPath('//button[contains(@type, "submit")]')

  # Check the result 
  if GetVarType(arr) != varNull:
    # Click the first element that was found 
    arr[0].Click() # Note we refer to the array item 
  else: 
    # If nothing was found, post a message to the log 
    Log.Message("Nothing was found.")

VBScript

Sub evalXPath
   Dim url, page, arr, i

   ' Obtain the Page object
   url = "http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_button_type"
   Browsers.Item(btIExplorer).Run url
   Set page = Sys.Browser("*").Page("*")

   ' Call the function
   arr = page.EvaluateXPath("//button[contains(@type, 'submit')]")

   ' Check the result
   If Not IsNull(arr) Then 
     ' Click the first element that was found
       arr(0).Click ' Note we refer to the array item

   Else 
     ' If nothing was found, post a message to the log
     Log.Error "Nothing was found."
   End If 
End Sub 

DelphiScript

procedure evalXPath;
var url, page, arr, i;
begin 
    // Obtain the Page object
   url := 'http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_button_type';
   Browsers.Item(btIExplorer).Run(url);
   page := Sys.Browser('*').Page('*');


   // Call the function
   arr := page.EvaluateXPath('//button[contains(@type, "submit")]');

   // Check the result
   if GetVarType(arr) <> varNull then
   begin
     // ' Click the first element that was found
     arr[0].Click; // Note we refer to the array item
   end
   else 
     begin 
       // If nothing was found, post a message to the log
       Log.Message('Nothing was found.');
     end;
end;

C++Script, C#Script

function evalXPath()
{

  // Obtain the Page object
  var url = "http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_button_type";
  Browsers["Item"](btIExplorer)["Run"](url);
  var page = Sys["Browser"]("*")["Page"]("*");

  // Call the function
  var tmp = page["EvaluateXPath"]("//button[contains(@type, 'submit')]");

  // Check the result
  if (tmp != null)
  {
    // If the element was found,
    // convert the array to the JScript-compatible format
    var arr = tmp["toArray"]();
    // and click the first element that was found
   arr[0]["Click"](); // Note we refer to the array item
  }
  else 
  {
    // If nothing was found, post a message to the log
    Log["Error"]("Nothing was found.");
  }
}

Mobile

JavaScript

function evalXPath1()
{
  // Obtain the Page object
  let url = "http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_button_type";

  // Select the mobile device
  Mobile.SetCurrent("MyDevice");

  // Obtain the WebView object and navigate to a web page
  // On Android devices
  let app = Mobile.Device().Process("smartbear.tctests.webbrowserapp");
  let page = app.RootLayout("").Layout("layoutTop").WebView("webview").Page("file:///android_asset/page1.html").ToUrl(url);
  // On iOS devices
  // let app = Mobile.Device().Process("SampleWebViewApp");
  // let page = app.Window(0).WebView(0).Page("http://www.example.com").ToUrl(url);
  
  // Call the function
  let arr = page.EvaluateXPath("//button[contains(@type, 'submit')]");

  // Check the result
  if (!strictEqual(arr, null))
  {
    // and click the first element that were found
    arr[0].Click(); // Note we refer to the array item
  }
  else 
  {
    // If nothing was found, post a message to the log
    Log.Error("Nothing was found.");
  }
}

JScript

function evalXPath1()
{
  // Obtain the Page object
  var url = "http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_button_type";

  // Select the mobile device
  Mobile.SetCurrent("MyDevice");

  // Obtain the WebView object and navigate to a web page
  // On Android devices
  var app = Mobile.Device.Process("smartbear.tctests.webbrowserapp");
  var page = app.RootLayout("").Layout("layoutTop").WebView("webview").Page("file:///android_asset/page1.html").ToUrl(url);
  // On iOS devices
  // var app = Mobile.Device.Process("SampleWebViewApp");
  // var page = app.Window(0).WebView(0).Page("http://www.example.com").ToUrl(url);
  
  // Call the function
  var tmp = page.EvaluateXPath("//button[contains(@type, 'submit')]");

  // Check the result
  if (tmp != null)
  {
    // Convert the array to the JScript-compatible format
    var arr = tmp.toArray();
    // and click the first element that was found
    arr[0].Click(); // Note we refer to the array item
  }
  else 
  {
    // If nothing was found, post a message to the log
    Log.Error("Nothing was found.");
  }
}

Python

def evalXPath1():
  # Obtain the Page object 
  url = "hhttp://www.w3schools.com/tags/tryit.asp?filename=tryhtml_button_type"

  # Select the mobile device
  Mobile.SetCurrent("MyDevice")

  # Obtain the WebView object and navigate to a web page 
  # On Android devices 
  app = Mobile.Device().Process("smartbear.tctests.webbrowserapp")
  page = app.RootLayout("").Layout("layoutTop").WebView("webview").Page("file:///android_asset/page1.html").ToUrl(url)
  # On iOS devices 
  # app = Mobile.Device().Process("SampleWebViewApp");
  # page = app.Window(0).WebView(0).Page("http://www.example.com").ToUrl(url);

  # Call the function 
  arr = page.EvaluateXPath('//button[contains(@type, "submit")]')

  # Check the result 
  if (GetVarType(arr) != varNull):
    # Click the first element that was found 
    arr[0].Click() # Note we refer to the array item  
  else: 
    # If nothing was found, post a message to the log 
    Log.Message('Nothing was found.')

VBScript

Sub evalXPath1
  Dim url, app, page, arr, i

  ' Obtain the Page object
  url = "http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_button_type"
  ' Select the mobile device
  Mobile.SetCurrent("MyDevice")
  ' Obtain the WebView object and navigate to a web page
  ' On Android devices
  Set app = Mobile.Device.Process("smartbear.tctests.webbrowserapp")
  Set page = app.RootLayout("").Layout("layoutTop").WebView("webview").Page("file:///android_asset/page1.html").ToUrl (url)
  ' On iOS devices
  ' Set app = Mobile.Device.Process("SampleWebViewApp")
  ' Set page = app.Window(0).WebView(0).Page("http://www.example.com").ToUrl (url)
  
  ' Call the function
  arr = page.EvaluateXPath("//button[contains(@type, 'submit')]")

  ' Check the result
  If Not isNull(arr, null) Then 
    ' Click the first element that was found
    arr(0).Click ' Note we refer to the array item
    Next
  Else 
    ' If nothing was found, post a message to the log
    Log.Error "Nothing was found."
  End If 
End Sub 

DelphiScript

procedure evalXPath1;
var url, app, page, arr, i;
begin 
  // Obtain the Page object
  url := 'http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_button_type';

  // Select the mobile device
  Mobile.SetCurrent('MyDevice');

  // Obtain the WebView object and navigate to a web page
  // On Android devices
  app := Mobile.Device.Process('smartbear.tctests.webbrowserapp');
  page := app.RootLayout('').Layout('layoutTop').WebView('webview').Page('file:///android_asset/page1.html').ToUrl(url);
  // On iOS devices
  // app := Mobile.Device.Process('SampleWebViewApp');
  // page := app.Window(0).WebView(0).Page('http://www.example.com').ToUrl(url);

  // Call the function
  arr := page.EvaluateXPath('//button[contains(@type, "submit")]');

  // Check the result
  if GetVarType(arr) <> varNull then
  begin
    // ' Click the first element that was found
    arr[0].Click; // Note we refer to the array item
  end
  else 
  begin 
    // If nothing was found, post a message to the log
    Log.Message('Nothing was found.');
  end;
end;

C++Script, C#Script

function evalXPath1()
{
   // Obtain the Page object
  var url = "http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_button_type";

  // Select the mobile device
  Mobile["SetCurrent"]("MyDevice");

  // Obtain the WebView object and navigate to a web page
  // On Android devices
  var app = Mobile["Device"].Process("smartbear.tctests.webbrowserapp");
  var page = app["RootLayout"]("")["Layout"]("layoutTop")["WebView"]("webview")["Page"]("file:///android_asset/page1.html")["ToUrl"](url)
  // On iOS devices
  // var app = Mobile["Device"].Process("SampleWebViewApp");
  // var page = app["Window"](0)["WebView"](0)["Page"]("http://www.example.com")["ToUrl"](url)
  
   // Call the function
   var tmp = page["EvaluateXPath"]("//button[contains(@type, 'onclick')]");

   // Check the result
   if (tmp != null)
   {
     // If the element was found,
     // convert the array to the JScript-compatible format
     var arr = tmp["toArray"]();
     // and click the first element that was found
      arr[0]["Click"](); // Note we refer to the array item
   }
   else 
   {
     // If nothing was found, post a message to the log
     Log["Error"]("Nothing was found.");
   }
}

FindChildByXPath Example

The following code snippet demonstrates how you can use the FindChildByXPath function to get an INPUT web element whose type attribute is equal to button. Note that the SearchInFrames parameter is set to False, that is, the function excludes frame content from the search. After the search is done, the sample code simulates a click on the found INPUT element.

Web

JavaScript

function evalChildByXPath()
 {

    // Obtain the Page object
    let url = "http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_button_type";
    Browsers.Item(btIExplorer).Run(url);
    let page = Sys.Browser("*").Page("*");

    // Call the function
    let obj = page.FindChildByXPath("//button[@type='submit']", true);

    // Check the result
  if (!strictEqual(obj, null))
    {
      // If the element was found, click it
      obj.Click();
   }
    else
    {
    // If the element was not found, post a message to the log
      Log.Error("The element was not found.");
    }
 }

JScript

function evalChildByXPath()
 {

    // Obtain the Page object
    var url = "http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_button_type";
    Browsers.Item(btIExplorer).Run(url);
    var page = Sys.Browser("*").Page("*");

    // Call the function
    var obj = page.FindChildByXPath("//button[@type='submit']", true);

    // Check the result
  if (obj != null)
    {
      // If the element was found, click it
      obj.Click();
   }
    else
    {
    // If the element was not found, post a message to the log
      Log.Error("The element was not found.");
    }
 }

Python

def evalChildByXPath():
  # Obtain the Page object
  url = "http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_button_type"
  Browsers.Item[btIExplorer].Run(url)
  page = Sys.Browser("*").Page("*")
 
  # Call the function
  obj = page.FindChildByXPath('//button[@type="submit"]', True)
 
  # Check the result 
  if (GetVarType(obj) != varNull):
    # If the element was found, click it
    obj.Click()
  else:
    # If the element was not found, post a message to the log
    Log.Message('The element was not found.')

VBScript

Sub evalChildByXPath
    Dim url, page, obj
 
   ' Obtain the Page object
    url = "http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_button_type"
    Browsers.Item(btIExplorer).Run url
    Set page = Sys.Browser("*").Page("*")
 
   ' Call the function
    Set obj = page.FindChildByXPath("//button[@type='submit']", True)
 
   ' Check the result
  If Not obj is Nothing Then
      ' If the element was found, click it
      obj.Click
    Else
      ' If the element was not found, post a message to the log
      Log.Error "The element was not found."
    End If
 End Sub
 

DelphiScript

procedure evalChildByXPath;
 var url, page, obj;
 begin
    // Obtain the Page object
    url := 'http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_button_type';
    Browsers.Item(btIExplorer).Run(url);
    page := Sys.Browser('*').Page('*');
 
   // Call the function
    obj := page.FindChildByXPath('//button[@type="submit"]', true);
 
   // Check the result
  if GetVarType(obj) <> varNull then
      begin
        // If the element was found, click it
        obj.Click;
      end
    else
      begin
        // If the element was not found, post a message to the log
        Log.Message('The element was not found.');
      end;
 end;

C++Script, C#Script

function evalChildByXPath()
 {
 
   // Obtain the Page object
    var url = "http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_button_type";
    Browsers["Item"](btIExplorer)["Run"](url);
    var page = Sys["Browser"]("*")["Page"]("*");
 
   // Call the function
    var obj = page["FindChildByXPath"]("//button[@type='submit']", true);
 
   // Check the result
  if (obj != null)
    {
      // If the element was found, click it
      obj["Click"]();
   }
    else
    {
    // If the element was not found, post a message to the log
      Log["Error"]("The element was not found.");
    }
 }

Mobile

JavaScript

function evalChildByXPath1()
{
  // Obtain the Page object
  let url = "http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_button_type";

  // Select the mobile device
  Mobile.SetCurrent("MyDevice");

  // Obtain the WebView object and navigate to a web page
  // On Android devices
  let app = Mobile.Device().Process("smartbear.tctests.webbrowserapp");
  let page = app.RootLayout("").Layout("layoutTop").WebView("webview").Page("file:///android_asset/page1.html").ToUrl(url);
  // On iOS devices
  // let app = Mobile.Device().Process("SampleWebViewApp");
  // let page = app.Window(0).WebView(0).Page("http://www.example.com").ToUrl(url);

  // Call the function
  let obj = page.FindChildByXPath("//button[@type='submit']", true);

  // Check the result
  if (!strictEqual(obj, null))
  {
    // If the element was found, click it
    obj.Click();
  }
  else
  {
    // If the element was not found, post a message to the log
    Log.Error("The element was not found.");
  }
}

JScript

function evalChildByXPath1()
{
  // Obtain the Page object
  var url = "http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_button_type";

  // Select the mobile device
  Mobile.SetCurrent("MyDevice");

  // Obtain the WebView object and navigate to a web page
  // On Android devices
  var app = Mobile.Device.Process("smartbear.tctests.webbrowserapp");
  var page = app.RootLayout("").Layout("layoutTop").WebView("webview").Page("file:///android_asset/page1.html").ToUrl(url);
  // On iOS devices
  // var app = Mobile.Device.Process("SampleWebViewApp");
  // var page = app.Window(0).WebView(0).Page("http://www.example.com").ToUrl(url);

  // Call the function
  var obj = page.FindChildByXPath("//button[@type='submit']", true);

  // Check the result
  if (obj != null)
  {
    // If the element was found, click it
    obj.Click();
  }
  else
  {
    // If the element was not found, post a message to the log
    Log.Error("The element was not found.");
  }
}

Python

def evalChildByXPath1():
  # Obtain the Page object
  url = "http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_button_type";

  # Select the mobile device
  Mobile.SetCurrent('MyDevice')

  # Obtain the WebView object and navigate to a web page 
  # On Android devices 
  app = Mobile.Device().Process("smartbear.tctests.webbrowserapp")
  page = app.RootLayout("").Layout("layoutTop").WebView("webview").Page("file:///android_asset/page1.html").ToUrl(url)
  # On iOS devices 
  # app = Mobile.Device().Process("SampleWebViewApp#)
  # page = app.Window(0).WebView(0).Page("http://www.example.com").ToUrl(url)

  # Call the function
  obj = page.FindChildByXPath('//button[@type="submit"]', True)
 
  # Check the result 
  if (GetVarType(obj) != varNull):
    # If the element was found, click it
    obj.Click()
  else:
    # If the element was not found, post a message to the log
    Log.Message('The element was not found.')

VBScript

Sub evalChildByXPath1
  Dim url, app, page, obj
 
  ' Obtain the Page object
  url = "http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_button_type"

  ' Select the mobile device
  Mobile.SetCurrent("MyDevice")

  ' Obtain the WebView object and navigate to a web page
  ' On Android devices
  Set app = Mobile.Device.Process("smartbear.tctests.webbrowserapp")
  Set page = app.RootLayout("").Layout("layoutTop").WebView("webview").Page("file:///android_asset/page1.html").ToUrl (url)
  ' On iOS devices
  ' Set app = Mobile.Device.Process("SampleWebViewApp")
  ' Set page = app.Window(0).WebView(0).Page("http://www.example.com").ToUrl (url)
 
  ' Call the function
  Set obj = page.FindChildByXPath("//button[@type='submit']", True)
 
  ' Check the result
  If Not obj is Nothing Then
    ' If the element was found, click it
    obj.Click
  Else
    ' If the element was not found, post a message to the log
    Log.Error "The element was not found."
  End If
End Sub

DelphiScript

procedure evalChildByXPath1;
var 
  url, page, obj;
begin
  // Obtain the Page object
  url := 'http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_button_type';

  // Select the mobile device
  Mobile.SetCurrent('MyDevice');

  // Obtain the WebView object and navigate to a web page
  // On Android devices
  app := Mobile.Device.Process('smartbear.tctests.webbrowserapp');
  page := app.RootLayout('').Layout('layoutTop').WebView('webview').Page('file:///android_asset/page1.html').ToUrl(url);
  // On iOS devices
  // app := Mobile.Device.Process('SampleWebViewApp');
  // page := app.Window(0).WebView(0).Page('http://www.example.com').ToUrl(url);

  // Call the function
  obj := page.FindChildByXPath('//button[@type="submit"]', true);
 
  // Check the result
  if GetVarType(obj) <> varNull then
  begin
    // If the element was found, click it
    obj.Click;
  end
  else
  begin
    // If the element was not found, post a message to the log
    Log.Message('The element was not found.');
  end;
end;

C++Script, C#Script

function evalChildByXPath1()
{
 
  // Obtain the Page object
  var url = "http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_button_type";

  // Select the mobile device
  Mobile["SetCurrent"]("MyDevice");

  // Obtain the WebView object and navigate to a web page
  // On Android devices
  var app = Mobile["Device"].Process("smartbear.tctests.webbrowserapp");
  var page = app["RootLayout"]("")["Layout"]("layoutTop")["WebView"]("webview")["Page"]("file:///android_asset/page1.html")["ToUrl"](url)
  // On iOS devices
  // var app = Mobile["Device"].Process("SampleWebViewApp");
  // var page = app["Window"](0)["WebView"](0)["Page"]("http://www.example.com")["ToUrl"](url)

  // Call the function
  var obj = page["FindChildByXPath"]("//button[@type='submit']", false);

  // Check the result
  if (obj != null)
  {
    // If the element was found, click it
    obj["Click"]();
  }
  else
  {
    // If the element was not found, post a message to the log
    Log["Error"]("The element was not found.");
  }
}

See Also

Find Web Objects
Access Native Web Attributes and Methods
EvaluateXPath Method (Page Objects)
Default Web Testing

Highlight search results