Dynamic object:
If
any object/ element properties keep on changing every time, we can say the
element is dynamic element.
When dealing
with dynamic objects in Selenium, where element properties like id change with each page
load, we can use XPath functions like contains()
and starts-with() to
locate these elements.
ex:
<input
id="userid101">
<!--
Upon reopening or refreshing the page, the id changes -->
<input
id="userid78">
When
we open page again, inspect the element, it shows dynamic values for id.
Handling Dynamic Objects Using XPath:
To locate
elements with dynamic properties, we can use:
- contains(): This function checks if
the attribute contains a specific substring.
- starts-with(): This function checks if
the attribute starts with a specific substring.
Note: XPath does not have an ends-with() function.
1. contains(@attrname,
'attrVal'):
syntax
--> //tagname[contains(@attrName,'AtrrValue')]
goes
to all tag names where attribute name
contains given attribute val.
html
:
<input
type="text" name="mname123" id="mid">
<input
type="text" name="mname345" id="mid">
Navigate
to input tag where 'name' contains 'mname' --> //input[contains(@attrNAme,
'Attr value')]
It goes to all input tags where 'name'
contains 'mname'
2. starts-with():
syntax--> //tagname[starts-with(@AttrName,
'AttrValue')]
goes
to all i/p tags where attribute name starts -with given value
<input
type="text" name="mname123" id="mid">
<input
type="text" name="mname345" id="mid">
- 1 ele
It goes to all input tags where name start
with 'mname' - //input[starts-with(@name,'mname')]
//input[starts-with(@name,'m')] - 2 ele
//input[starts-with(@name,'mn')] - 1 ele
//input[starts-with(@name,'mna')] - 1 ele
code:
package
WebelementsBasics;
import
org.openqa.selenium.By;
import
org.openqa.selenium.WebDriver;
import
org.openqa.selenium.chrome.ChromeDriver;
public
class DynamicTextbox {
public static void main(String[]
args) throws InterruptedException {
//
open chrome
// open Chrome browser
// 1.
System.setProperty("webdriver.chrome.driver",
".\\Drivers\\chromedriver.exe");
//2 create obj for ChromeDriver- Class
WebDriver driver = new ChromeDriver();// open chrome browser
with empty url
driver.get("file:///C:/brahma/Practise/SelniumPractiseNew/SampleWebpage.html");
Thread.sleep(4000);
// enter data in 'middle
name' textbox using mname345 -use
contains
// driver.findElement(By.name("mname901")).sendKeys("Ram");
// It throws NosuchElement
Exception.
// if there is no
element found on the page by using name = mname901, then it throws
NosuchElement Exception.
// enter data in middle
name using contains()
driver.findElement(By.xpath("//input[contains(@name,'mname')]")).sendKeys("Rama");
// starts-with()
driver.findElement(By.xpath("//input[starts-with(@name,'mname')]")).sendKeys("Sita");
}
}
FAQ***
how can you handle dynamic object/ elements?
Identifying Elements Using the last() Function in XPath:
Identify
all input tags à
//input - 18 ele
This XPath
expression selects all input elements in the document
identify
last input tag à
//input[18] (or) //input[last()]
This XPath
expression selects the last input element in the document.
//input[last()-1] - submit à to identify the second
last input element.
//input[last()-2] -- login à to
identify the third last input element.
ex2:
Identify
select tags in htmlpage --> //select
- 3 select tags
//select[last()]
-MultiSelect
//select[last()-1] - bikes dropdown
//select[last()-2] - cars dropdown
ex3:
Get
all textboxes --> //input[@type='text'] 6 ele
<input
type ='text' >
ex4:HW xpath to get all radio buttons
ex5:
HW xpath to get all checkboxes
ex
6: xpath to get all links --> //a
3 ele
<a
href= "url" > google linktext </a>
//a
1st
'a' tag à //a[1]
2nd
'a' tag à //a[2]
3rd
'a' tag à //a[3]
Position Function in XPath
The position() function
in XPath is used to specify the position of an element within its parent. This
can be useful for selecting a specific element when there are multiple elements
of the same type.
Position():
ex: //input[1] à 1st input tag
//input[position()=1] or //input[1]
Selects the
first <input> tag in
the document.
//input[position()=2]
or //input[2]
Selects the
second <input> tag in
the document
//a[position()=1]
- Selects the first <a>
(anchor) tag in the document.
//a[position()=2]
- Selects the second <a>
tag in the document.
//a[position()=3]
- Selects the third <a>
tag in the document.
//a[position()=50]
- o ele
XPath Using Text Function:
The text() function in XPath is
used to select elements based on their visible text content.
This can be
particularly useful when an element does not have identifiable attributes like id, name, class, or href.
My
gmail link :
<a
href="https://www.google.com/intl/en-GB/gmail/about/"> My gmail
</a>
//a[@href='https://www.google.com/intl/en-GB/gmail/about/'] - 1 ele
<a
> My gmail </a>
no id,no
name, no class, no href -->
<a>
My gmail</a>
syntax
--> //tagname[text()='visible text
from page']
xpath
for My gmail link --> //a[text()='My gmail'] 1 ele
xpath
for Facebook --> //a[text()='Open Facebook'] 1 ele
HW
Write xpath using text() for 'Open amazon' link -->
HW
xpath for 'MyButton' button using text() --> 1 ele
HW
xpath for para using text()-->
Note:
------
if we don't have name, id, class, any attribute name , we can identify the element based on text()
<button>MyButton</button>
Using contains(text(), 'visible text from
page') in XPath:
The contains() function in XPath
allows you to select elements based on whether their text contains a given
substring. This is useful for handling elements with dynamic or partially known
text.
Syntax-->
//tagname[contains(text(),'visible text from page')]
<a
href="https://www.google.com/intl/en-GB/gmail/about/">My
gmail</a>
Full text:
My
gmail --> //a [contains(text(),'My gmail')] - goes to all 'a' tags where text
contains
Partial text:
given
value 'My gmail --> //a
[contains(text(),'gmail')]
My
--> //a [contains(text(),'My')]
Here
we can give full text (or) partial text from page.
Open
Facebook Link:
//a[text()='Open
Facebook '] -- o ele as we have some spaces in page
//a[contains(text(),'Open
Facebook')] -- 1 ele it ignores spaces here
HW Write XPath for the following using contains(text(),''):
facebook
-->
book
-->
Open
amazon -->
amazon -->
Child and Sub-child Navigation in XPath:
//
- child (or) sub child:
/
is used to navigate to child tag name/ child element
ex: html/body
// is used to navigate to
child or sub-child tags (grandchildren and beyond).
ex:
/html//title
/html
// input -- child input tags or subchild tags of input
//body/select[1]/option
//body//option - 14
navigate to child/ sub child tags of 'option'
/ ànavigate to child
tag
//
à child tag or Sub child tag
XPath Using Wildcards (*) for Tag and
Attribute Names:
In XPath,
the asterisk (*) wildcard character
is used to represent any tag name or any attribute name. This flexibility
allows XPath queries to target elements regardless of their specific tag names
or attribute names, which is particularly useful when dealing with dynamic or
changing attributes.
--> Tag as *
--> Attribute name as *
--> any tag name and any attribute
ex1
: Tag as *
-------------
//input[@id='fid'] - 2
//Select[@id='fid']
- 0
//
* [@id='fid'] - any tag name where id =
'fid'
To select
any tag with an id
attribute equal to 'fid':
* represents any tag name i.e could be input,
select,a etc...
//*[@class='firstclass']
- 2 le
meaning
--> *
means any tag name
here
it refers any tag name where class ='firstclass']
HW
XPath for 'any tag' name where id = mid?
HW
Xpath for 'any tag' name whose name=
'cars'?
XPath Using Wildcards for Attribute Names:
In XPath,
the asterisk (*) wildcard character
can also be used to represent any attribute name. This allows for XPath queries
that target elements based on any attribute they might have, regardless of its
specific name.
ex2:
Attribute name as *
-------------------------
//input[@id='fid'] -->
//input[@*='fid']
//input[@name='male']
--> //input[@*='male']
//input[@*='lname'] = 1 ele
*
--> any Attribute name / property name i.e id, name,class.... etc
Xpath for input tag name , any attribute name = 'pname' ?
//input[@*='pname']
Xpath
for select tag name , any attribute
name = 'cars'
//select[@*='cars']
HW Xpath for select tag name , any attribute name = 'bikes'
HW Xpath for select tag name , any attribute name = 'maleid'
XPath for Any Tag Name and Any Attribute:
In XPath,
you can use the wildcard *
to represent any tag name and any attribute name within an expression.
//input[@name='fname']
//*[@*='fname']
// any tag name , any attribute name = 'fname'
//*[@*='pname'] - 1 ele
any
tag name any attr = 'pname '
//a[text()='My
gmail']
//*[text()='My
gmail']
* --> any tag name - whose text= My gmail
Xpath
any tag and any attribute =
'Login'
HW
Xpath any tag and any attribute = civil'
HW
Xpath any tag and any attribute = eleId
XPath using Or (|)
In XPath, the | (pipe)
operator is used to combine two or more XPath expressions to select elements
that satisfy either condition. This is particularly useful when you want to
find elements based on multiple attributes or conditions.
find
element where name = fname' (or) name='lname' ?
--> //input[@name='fname'] |
//input[@name='lname']
identifies
elements where name = fname or name =
'lname' 2 ele
Xpath , input tag ,where name ='mname' or id='lid'?
//input[@name='mname'] |
//input[@id='lid'] - 1 ele
HW
Xpath , select tag ,where name ='cars'
or id='bikesid'?
HW
Xpath , select tag ,where name ='cars'
or id='bikid'?
Using and with Multiple Properties in XPath:
When you
want to specify multiple conditions for selecting elements using XPath, you can
use the and operator along
with multiple properties or attributes. Each condition inside square brackets [] represents a property or
attribute condition that must be satisfied by the selected element.
and
- - give Multiple properties [] [] []
…. Etc
//input[@name='fname'][@id='fid']
-- or //input[@name='fname' and @id='fid']
//input[@name='fname'][@id='fid']
[@class='firstclass'] .......
or
//input[@name='fname'
and @id='fid'and @class='firstclass']
//input[@class='firstclass']
[@id='fid']
//input[@class='firstclass']
[@id='fid'] [@name='fname']
Xpath , input tag where id='uid' and name= 'uname'?
//input[@id='uid'][@name='uname']
HW
Xpath , input tag where id='mid' and
name= 'uname'?
0 ele
HW
Xpath Select tag where name ='cars' and
id=carid
HW
Xpath Select tag where name ='cars' and
id=bikesid
HW
Xpath input tag where name ='lname' and id=lid
and class='firstclass'?
Write
xpath,how many checkboxes ?
<input type = "text" -- text box
<input type
= "radio" -- radio button
<input type = "checkbox" -- check box
xpath
--> //input[@type='checkbox'] - 4 ele
HW:
xpath how many textboxes ?
<input type = "text" -- text box
xpath -->
HW
:xpath , how many radio btns?
<input
type = 'Radio'
//
HW how many links ?
xpath
-->
HW xpath , how many dropdowns are there?
select
xpath
-->
//
HW xpath , how many images ?
HW
WAP to use all above xpath's in textbox
,radio , checkboxes....?