Selenium Notes

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Selenium

THE LIST OF SELENIUM LOCATORS:


1. By.tagName
Driver.findElement(By.tagName("a")).click();
In the browser find the element by tag name ‘a’ and click on it
2. By.id
driver.findElement(By.id("a1")).click();
3. By.name
driver.findElement(By.name("n1")).click();
4. By.className
driver.findElement(By.className("c1")).click();
5. By.linkText
driver.findElement(By.linkText("actitime")).click();
note: the locator ‘linkText’ can be used only if the element is a link
6. By.partialLinkText
driver.findElement(By.partialLinkText("acti")).click();
note: this locator is used to handle dynamic links
driver.findElement(By.partialLinkText("Inbox")).click();
driver.findElement(By.partialLinkText("Bu/8ildNo")).click
7. By.cssSelector
Tag & ID
driver.findElement(By.cssSelector("input#email")).sendKeys("David");
driver.findElement(By.cssSelector("#email")).sendKeys("David");

Tag & Class


driver.findElement(By.cssSelector("input.inputtext")).sendKeys("John");
driver.findElement(By.cssSelector(".inputtext")).sendKeys("John");

Tag & attribute


driver.findElement(By.cssSelector("[name=email]")).sendKeys("Smith");
driver.findElement(By.cssSelector("input[name=email]")).sendKeys("Smith"
);
Tag , class & attribute
driver.findElement(By.cssSelector("input.inputtext[data-
testid=royal_email]")).sendKeys("Smith");
driver.findElement(By.cssSelector("input.inputtext[data-
testid=royal_pass]")).sendKeys("abc");

By.xpath
What is Xpath ?
 Xpath is defined as XML path.
 It is a syntax or language for finding any element on the web page using
xml path expression.
 Xpath is used to find the location of any element on a webpage using
HTML DOM structure
 Xpath is can used to navigate through elements and attributes in DOM

Type of Xpath
1. Absolute Xpath/ Full Xpath
In absolute xpath we need to navigate form the root tag through each
and every node till that element found in that particular node
 Absolute xpath starts from root node
 Absolute xpath start with /
 In Absolute xpath we use only tags/nodes
 Absolute xpath is full path of xml path
 /html/body/div[1]/div/div/[3]/div[1]/img

2. Relative Xpath /Partial Xpath


In case relative xpath we don’t navigate to each and every node so
relative xpath work based on attribute and value and property
 Relative xpath directly jump to element on Dom.
 Relative xpath start with //
 In Relative xpath we use attributes
 Absolute xpath is a partial xpath
 //*[@id=”divLogo”]/img
Syntax: //tagname[@attribute=’value’]
Which Xpath is preferred ? why ?
Ans: Relative xpath always preferred to use because if developer add new
element of done some changes in nodes or move that node in other place at
that time absolute xpath chain will be broken so we can’t go for absolute xpath
And relative xpath is partial xpath it is best way to capture specific node
1. Or
Either first attribute or second attribute will be math then it located the
element
Ex: input [@id=’search_query_top’ or @name=’search_query1’ ]
2. And
Both attributes should be match otherwise then it will be locate the
element
Ex: input [@id=’search_query_top’ and @name=’search_query1’ ]

3. Contains() or Starts-with()
Contains is a method which will verify then attribute value is a part of
actual values then it will locate the element
Syntax: tagname[contains(@attribute,’value’)]
Ex: input[@id=’firstname’]….basic xpath
Input[contains(@attribute,’first’)]……..using contains()
//input[@id=’start’]
//input[@id=stop]
//input[contains(@id,’st’)]……….Dynamic xpath
//input[starts-with(@id,’st’)]……….Dynamic xpath
(//li[contains(text(),'8 GB RAM | 128 GB ROM | Expandable Upto 256 GB')] )
[1]/../../..//div[text()='realme 9 (Sunburst Gold, 128 GB)']
4. Text()
Text() is used to capture the text present in html node and also to check
the equality and to locate text element
Ex://a[text()=’Women’]
5. Chained xpath
Chained xpath used to xpath forward by another xpath
(we can capture from parent to child if similar element node
present with the help of number )
Ex: //form[@id=’searchbox’]//input[@id=’search_query_top’]
or
Ex: //form[@id=’searchbox’]//input[4]
Xpath Axes:
Self:
Current html node which we select or current html tag
Child:
Traverse all child element of the current html tag
Syntax: //*[attribute=’value’]/chid::tagname
Parent:
Traverse parent element of the current html tag
Syntax: //*[attribute=’value’]/parent::tagname
Following:
Traverse all the element that comes after the current tag
Syntax: //*[attribute=’value’]/following:: tagname
Preceding:
Traverse all nodes that comes before the current html tag
Syntax: //[attribute=’value’]/preceding:: tagname
Following-sibling :
Traverse from current html tag to next sibling html tag
Syntax: //current html tag[@attribute=;’value’]/following-sibling::previous
tag[@attribut=’value’]
Preceding-sibling:
Traverse from current html tag to previous sibling html tag
Syntax: //current html tag[@attribute=;’value’]/preceding-sibling::previous
tag[@attribut=’value’]
Ancestor:
Traverse all the ancestor elements (grandparent, parent, etc) of the
current html tag
Syntax: //*[attribute=’value’]/ancestor:: tagname
Descendant:
Traverse all descendent element (child nod , grandchild node, etc.) of
the current html tag
Syntax: //*[attribute =’value’]/Descendant::tagname

You might also like