Pages

Locators in selenium

What are Locators available in selenium ?

Locators: To locate/detect the elements of a any web page , we will use Selenium locators.

Following are different types of locators and preferences to locators:

1) If  you are unable to locate web element by using "id" , then go to next step ,
2) If  you are unable to locate web element by using "name" , then go to next step ,
3) If  you are unable to locate web element by using "css" , then go to next step ,
4) If  you are unable to locate web element by using "xpath" .

Apart from above locators, we have below locators as well. Depending upon the context we will use below locators:

5) If  you are unable to locate web element by using "className" , then go to next step ,
6) If  you are unable to locate web element by using "tagName" , then go to next step ,

If we want to locate link web elements (<a> in html) ,then use below locators are preferred: 

7) If  you are unable to locate web element by using "linkText" , then go to next step ,
8) If  you are unable to locate web element by using "partialLinkText" , then go to next step .

We will face some situations where we are unable to locate web page elements by using above all locators.At that time we will use "JavascriptExecutor"

Syntax for JavascriptExecutor:

JavascriptExecutor je=(JavascriptExecutor)driver;
je.executeScript(Script,args);

Where, Script - java script code need to execute
args - arguments to script (optional)

Example:

JavascriptExecutor je=(JavascriptExecutor)driver;
je.executeScript("alert('Keep Learning')");

Note: Will discuss more about JavascriptExecutor in new posts.

Please comment below to feedback or ask questions.

No comments:

Post a Comment

Please comment below to feedback or ask questions.