0% found this document useful (0 votes)
282 views4 pages

Q) Element Locators in Selenium

The document discusses the WebDriver interface in Selenium for automating browser interactions. It describes how the Driver object is used to control the browser and load pages. It then covers different types of elements that can be interacted with like links and text boxes. It lists various locator strategies for finding elements and methods for interacting with pages, like getting URLs and titles, refreshing, and entering text. It provides examples of launching a browser, loading a page, refreshing it, and closing the browser.

Uploaded by

Avirup Ray
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
282 views4 pages

Q) Element Locators in Selenium

The document discusses the WebDriver interface in Selenium for automating browser interactions. It describes how the Driver object is used to control the browser and load pages. It then covers different types of elements that can be interacted with like links and text boxes. It lists various locator strategies for finding elements and methods for interacting with pages, like getting URLs and titles, refreshing, and entering text. It provides examples of launching a browser, loading a page, refreshing it, and closing the browser.

Uploaded by

Avirup Ray
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 4

1) driver = Driver object

1) driver.get(“URL”);
 Opens the specified URL in the Browser Window.

Q) Web Elements and Locators.

1) Note :-
 For Handling Browser,Page  Driver object (i.e driver) is enough.
 For Remaining elements we have to use WebDriver methods

2) Some Manual Operations :-


 Launch Browser
 Navigate to a specified URL.
 Close Focussed Browser.
 Close all opened Browsers.
 Refresh the browser

 Navigate from one URL to another.


 Navigate back,forward
 Maximize,Minimize the browser etc…

3) Some
 Get Page Title ,
 Get Page Source(i.e HTML source) , Get Page URL , etc…

4) Some operations on EditBox


 Enter some value.
 Clear the value.
 Check enabled status (i.e whether the
 Check the existence.
 Get the value. etc….

5) Some operations on Link :-


 Click the link
 Check enabled status.
 Return link name.

6) Some operations on Image

Q) Element Locators in Selenium

In Selenium
Element locator value.

1) Locator :-
 It is an address that identifies a web element uniquely within the web page
2) Selenium supports 8 Element Locators to recognize Elements in the WebPage :-
 id,name
 tagName,className
 linkText,partialLinkText
 cssSelector
 xpath

3) How to inspect elements


 Firefox  Use Firebug and Firepath plugins.
 Google Chrome  No need to install any plugin , They have built in developer tools.

4) Element locators are common for all browsers.

Ex1) id

1) Syntax :- By.id(“id_value”);

Q) WebDriver methods (for performing operations on Elements)

1) driver.get(“URL”);
 Opens a specified URL in the Browser’s window.

2) String title = driver.getTitle()


 Returns title of the browser in the form of a String.

3) String var = driver.getPageSource();


 Returns the HTML page source of the provided URL.
 Returns in the fom of a String.

4) String var = driver.getCurrentUrl();


 Returns the current URL of the browser.
 Returns in the form of a String.

Q) Browser Navigation Methods.

1) driver.navigate().to(“URL”);
 To navigate to the page with the specified URL.

2) driver.close();
 To close the focussed browser.

Q) Illustration
 Launch a Browser  Open www.learncpp.com
 Wait for 10s ……Then close the browser

public static void main(String[] args) throws InterruptedException


{
// Starting the Google Chrome Browser
System.setProperty("webdriver.chrome.driver","G://Selenium Files/Selenium
Drivers/chromedriver.exe");
// Launch Google Chromew Browser with Blank Page
WebDriver driver = new ChromeDriver();
driver.get("https://www.learncpp.com/");
String url1 = driver.getCurrentUrl();
System.out.println(url1);

Thread.sleep(10000);

driver.close();// Closes the Browser


}

3) driver.quit();

3) driver.navigate().refresh()
 To refresh the Current webpage in the Browser

Q) Illustration
 Opens www.learncpp.com
 Refreshes that page
 Then Closes the browser
public static void main(String[] args)
{
// Starting the Google Chrome Browser
System.setProperty("webdriver.chrome.driver","G://Selenium Files/Selenium
Drivers/chromedriver.exe");

// Launch Google Chromew Browser with Blank Page


WebDriver driver = new ChromeDriver();
driver.get("https://www.learncpp.com/");
String url1 = driver.getCurrentUrl();
System.out.println(url1);

driver.navigate().refresh();

driver.close();// Closes the Browser


}

1) driver.findElement(By.ElementLocator(“value”)).sendKeys(“input_data“);
 sendKeys() method enters the inpue_data into the Editbox/Textbox
 input_data = String.
 Find the Editbox/Textbox using Element locators.
 Then the code becomes driver.element.sendKeys(“input_data”);
 Where element = Textbox / Editbox.
2) driver.findElement(By.ElementLocator(“value”)).clear();
 clear() method clears the data in the EditBox/Textbox
 Find the Editbox/Textbox using Element locators.
 Then the code becomes driver.element.sendKeys(“input_data”);
 Where element = Textbox / Editbox.

You might also like