Information in this topic applies to web and hybrid mobile applications. |
This topic explains how you can search for the needed elements on the tested web page by the elements’ attributes and their values.
General Information
To search for web page elements by their attributes, you can use various XPath expressions. By using the EvaluateXPath
and FindChildByXPath
methods, you can find web page elements by their names and attributes. The difference between these methods is that the EvaluateXPath
method returns an array of found objects, while the FindChildByXPath
method returns the first object that meets the specified criteria.
There can be issues when property and attribute values returned by the browser differ from those defined in the web page’s source code. For more information on such issues, see Important Notes. |
To search for the needed web page element, you must pass the appropriate XPath expression to the EvaluateXPath
or FindChildByXPath
method through its XPath parameter. The expression must comply with the XPath rules. You can find detailed information on the XPath syntax in the MSDN Library.
Tip: | Within XPath expressions passed to the EvaluateXPath or FindChildByXPath method, you can also use XPath standard functions like contains , starts-with and others that may let you create more flexible XPath expressions. |
If needed, you can limit the search scope by excluding frames from the search. Since frames can contain script-generated content, this can slow down the search process. To exclude frames from the search, use the SearchInFrames parameter. By default, it is set to True, that is, frames' content is not excluded from the search. If you set SearchInFrames to False, the contents of frames will be omitted.
The table below demonstrates some examples of XPath expressions which you can pass to the EvaluateXPath
(or FindChildByXPath
) method in order to search for the desired web page elements.
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. |
Also, the Examples section of this topic demonstrates how you can use the EvaluateXPath
and FindChildByXPath
methods to search for web page elements from your scripts. In keyword tests, you can call the EvaluateXPath
and FindChildByXPath
methods by using the Call Object Method or Run Code Snippet operation. Also, you can execute the entire script routine like the example below from your keyword test by using the Run Script Routine operation.
Tips on Using XPath Expressions
When using XPath expressions to search for web page elements with the EvaluateXPath
or FindChildByXPath
method, follow the tips below:
Note: | Though the rest of this section describes the specifics of EvaluateXPath , it can also be applied to FindChildByXPath . |
-
To search the entire web page for elements, put a double slash (//) at the beginning of the XPath expression. If you pass an expression without double slash at the beginning to the
EvaluateXPath
method, 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's point of view:
//img[@src = 'Image1.png']
//img[@src = 'image1.PNG']
//img[@src = 'IMAGE1.PNG']To avoid problems, 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’s
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 documentation of XPath’s
translate
function in the MSDN Library 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’scontains
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'] -
Note that the hierarchy of elements on a web page loaded to an Internet 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, if you rely only on the page’s source code and try to retrieve, for instance, all TR elements stored within such a table with the products identifier, the following XPath expression will fail because it will search for TR elements on the hierarchy level where the TBODY element will actually reside after the page is loaded to the browser:
//TABLE[@id='products']/TRTherefore, when searching for a certain web page element by using XPath expressions, keep in mind that some implicitly defined elements may appear in the hierarchy of web page elements when the page is loaded to the browser. If you are not sure whether some elements will appear on a certain hierarchy level, you can use any of the following workarounds:
-
Explore the actual tree of web page elements by using a special debugging tool for your web browser and then create your XPath expressions. For instance, in Internet Explorer 10 and later, you can use Developer Tools (press F12 in the browser to invoke the Tools). If you open your web page in Firefox, you can explore the actual hierarchy of web page elements by using the Firebug add-on for Firefox.
-
When you search for an element within another element specified in your XPath expression, and you are not sure whether some element will appear in the hierarchy between these elements, you can place a double slash (//) between the names of the elements specified in your expression:
//TABLE[@id='products']//TRThis will allow you to recursively search for the desired element at any depth within its ancestor element specified at the beginning of the expression.
-
Examples
EvaluateXPath Example
The example below demonstrates how you can use the EvaluateXPath
function 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
About Finding Objects on Web Pages
Accessing Native Web Attributes and Methods
EvaluateXPath Method (Page Objects)
Testing Web Applications