How to Handle Browser in Selenium Webdriver using Java

                   : Handling Browser in selenium :

  •  How to enter URL in browser using selenium webdriver ?

get() - In selenium webdriver , get method is used to open an 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 ?

maximize()- In selenium webdriver maximize method is used to maximize 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 way to refresh browser .sometimes in certain browser normal refresh command does not work, in that situation we use these command to overcome from the situation.

1- refresh ()- Refresh command in selenium is most frequently used command by the IT professionals . Refresh command is a navigation command in selenium webdriver.

Selenium Code : 

//Refresh Browser Window

 driver.navigate().refresh();

2-sendkey ()- Sendkeys method in selenium is also used for refreshing a webpage. We use sendkeys method on any text box on 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 java ?

back() - Back method in selenium is used to click back button of an browser. 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 java ?

forward () - Forward method in selenium in used to click forward button of browser. Forward command is a navigation command in selenium webdriver .

Selenium Code :

//Click browser forward Button

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

 driver.navigate().forward();


  • How to close browser window in selenium webdriver?

In selenium webdriver close () and quit () are two method 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 ()- quit method in selenium is used to close all the open browser window and end the webdriver session .

Selenium Code :

//Close all browser window

      driver.quit();


  • How to verify title in selenium Webdriver using java ?
getTitle() - get title method in selenium is used to get the title of 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 url 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 webdriver provides list of browser command which is listed below -
  • Navigate To Command -driver.navigate.to();
  • Forward Command- driver.navigate.forward();
  • Back Command - driver.navigate.back();
  • Refresh Command -refresh();

Hope !!! Above Tutorial of "how to handle browser in selenium using java " helpful for you ...

Team ,
QA acharya

Tags : Handling browser in selenium , Navigation method in selenium , how to open , close , maximize , window in selenium , Navigation command in selenium , browser handling in selenium webdriver , selenium navigation command .

How to handle browser in selenium  


Post a Comment

0 Comments