Italian Trulli

How To Handle Browser Action in Selenium Using Java

Test Tribe
.

                  Handling Browser Action in Selenium 


How to Launch Chrome Browser in Selenium Webdriver Using Java?

Just Follow the Below steps to launch the Chrome browser in Selenium.

  • First, Download the latest ChromeDriver binary from Chromium.org https://chromedriver.chromium.org/downloads  download page and place the executable on your local machine. 
  • Now Set the webdriver.chrome.driver property to the chromeDriver.exe’s location as- System.setProperty(“webdriver.chrome.driver”, “chromeDriver.exe path”);

Selenium Code: Launching Chrome Browser
Selenium code to launch the Chrome browser in selenium is given below. 

System.setProperty("webdriver.chrome.driver""C:\\Software\\chromedriver.exe");
WebDriver driver=new ChromeDriver();

The above selenium code will launch the Chrome browser after successful code execution.


 How to Enter URL in Browser Using Selenium Webdriver?

get() - In the Selenium web driver, the get method is used to open a URL.

Selenium Code :
//Enter URL in Browser
driver.get("https://www.facebook.com/");


How to Navigate to URL or How to Open a Webpage in Selenium Browser?

navigate().to()- In selenium webdriver navigate to method is used to load a new webpage in the current browser window.

Selenium Code :
//Navigate to URL
 driver.navigat().to("https://www.facebook.com/");


How to Maximize Browser Window in Selenium Webdriver?

maximize()- In the Selenium webdriver maximize method is used to maximize the browser window.

Selenium Code:
//Maximize Browser Window 
 driver.manage().window().maximize();


How To Refresh Browser in Selenium Webdriver Using Java?

In Selenium there are multiple ways to refresh the browser Sometimes in certain browsers normal refresh command does not work, in that situation we use these commands to overcome the situation.

1- refresh ()- The refresh command in selenium is the most frequently used command by IT professionals. The refresh command is a navigation command in the Selenium web driver.

Selenium Code : 
//Refresh Browser Window
 driver.navigate().refresh();

2-sendkey ()- The Sendkeys method in selenium is also used for refreshing a webpage. We use the send keys method on any text box on the webpage.

Selenium Code : 
//Refresh Browser Window
driver.get("https://www.facebook.com/"); 
driver.findElement(By.xpath("//input[@type='text']")).sendkeys(keys.F5);


How To Click Browser Back Button in Selenium Webdriver Using Java?

back() - The back method in selenium is used to click the back button of a browser. The back command is a navigation command in selenium.

Selenium Code :
//Click browser Back Button
driver.get("https://www.facebook.com/");
 driver.navigate().back();


How To Click Browser Forward Button in Selenium Webdriver Using Java?

forward () - The forward method in selenium is used to click the forward button of the browser. Forward command is a navigation command in the selenium web driver.

Selenium Code :
//Click browser forward Button
driver.get("https://www.facebook.com/");
 driver.navigate().forward();


How To Close Browser Window in Selenium Webdriver Using Java?

In Selenium, web driver close () and quit () are two methods to close the browser session. And knowing and understanding both of them is most important during the test execution.

1- close()- close method in selenium used to close the current browser window.

Selenium Code :
//Close Current browser window
 driver.close();

2-  quit ()- The quit method in Selenium is used to close all the open browser windows and end the web driver session.

Selenium Code :
//Close all browser window
 driver.quit();


How To Get Page Title in Selenium Webdriver Using Java?
getTitle() - The get title method in selenium is used to get the title of the page.

Selenium Code - 

//How to get and verify page title in selenium

String st= driver.getTitle();
System.out.println(st);
if (st.contains("Google")) 
{
System.out.println("Valid Title ");
}
else 
{
System.out.println("Invalid Title");
}

How to Get Current URLs in Selenium Using Java?

gerCurrentUrl()- To get the current URL in Selenium webdriver use gerCurrentUrl() method.

Selenium Code - 
//How to get and verify current URL
String getU=driver.getCurrentUrl();
System.out.println(getU);
if (getU.contains("https://www.google.com/")) 
{
System.out.println("Valid URL ");
}
else 
{
System.out.println("Invalid URL");
}

How To Get Page Source Code in Selenium using Java?

getPageSource() - To get the page source code in Selenium use getPageSource() method.

Selenium Code - 
// To get page source Code
String pgs=driver.getPageSource();
System.out.println(pgs);


Selenium Code to Automate Browser Window Using Java.

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
 
public class LaunchBrowser {
 
       public static void main(String[] args) {
              // TODO Auto-generated method stub
              System.setProperty("webdriver.chrome.driver""C:\\Software\\chromedriver.exe");
              WebDriver driver=new ChromeDriver();
             
              //Maximize Browser Window
              driver.manage().window().maximize();
             
              //Enter URL in Browser
              driver.get("https://www.facebook.com/");

              
//Navigate to URL

                        driver.navigat().to("https://www.facebook.com/");

              //Refresh Browser Window
              driver.navigate().refresh();
             
              //Click browser Back Button
              driver.navigate().back();
             
              //Click Browser Forword button
              driver.navigate().forward();
             
              //Close Current brwoser window
              driver.close();
             
              //Close All open browser  window
              driver.quit();
      
       }
 }

  • Navigation Command in Selenium Webdriver 
Selenium web driver provides a list of browser commands which are listed below -
  • Navigate To Command -driver.navigate.to();
  • Forward Command- driver.navigate.forward();
  • Back Command - driver.navigate.back();
  • Refresh Command -refresh();

Hope !!! The above Tutorial on how to handle browsers in Selenium using Java is helpful for you ...

Team,
QA acharya

How to handle browser in selenium  

Post a Comment

0 Comments