Selenium Java Tutorial
WebDriver Commands
What are the WebDriver Commands in Selenium ?
Selenium Webdriver provides a number of commands for intercating with the Webdriver and performing various tasks.
These commands usually methods are accessed by calling driver.methodName(); through a driver variable of WebDriver.
Commands are further categorized in three types of commands :
WebDriver-Browser Commands
WebDriver-Navigation Commands
WebDriver-WebElement Commands
Example Code for WebDriver-Browser Commands
package seleniumexamples;
import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver;
public class BrowserCommands {
public static void main(String[] args) { // TODO Auto-generated method stub WebDriver driver; String baseURL="https://www.oxfordreference.com/page/scienceandtech/science-and-technology"; //Launch the web browser System.setProperty("webdriver.chrome.driver",
"..\\SeleniumJava\\drivers\\chromedriver99.exe"); driver=new ChromeDriver(); //To maximize the size of window driver.manage().window().maximize(); // get() command to open a web page driver.get(baseURL); System.out.println("Web Page is Loaded..."); //getTitle() System.out.println("The Title of this Web Page is : "+driver.getTitle()); //getCurrentUrl() command to read the url of The webpage System.out.println("The URL of this Web Page is : "+driver.getCurrentUrl()); //close() command to close the current window driver.close(); System.out.println("Window Closed...");
}
}
Example Code for WebDriver-Navigation Commands
package seleniumexamples;
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver;
public class NavigationCommands {
public static void main(String[] args) { // TODO Auto-generated method stub WebDriver driver; String baseURL="https://www.oxfordreference.com/page/scienceandtech/science-and-technology"; //Launch the web browser System.setProperty("webdriver.chrome.driver",
"..\\SeleniumJava\\drivers\\chromedriver99.exe"); driver=new ChromeDriver(); //To maximize the size of window driver.manage().window().maximize(); //Navigate through url driver.navigate().to(baseURL); driver.findElement(By.cssSelector("a[href='/view/10.1093/acref/9780199657681.001.0001/acref-9780199657681-e-4110']")).click(); //Navigate back driver.navigate().back(); //Navigate forward driver.navigate().forward(); //Refresh the page driver.navigate().refresh(); //Close the current and all associated window driver.quit();
}
}
Example Code for WebDriver-WebElement Commands
package seleniumexamples;
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver;
public class WebElementCommands {
public static void main(String[] args) throws InterruptedException { // TODO Auto-generated method stub WebDriver driver; String baseURL="https://www.oxfordreference.com/page/scienceandtech/science-and-technology"; //Launch the web browser System.setProperty("webdriver.chrome.driver",
"..\\SeleniumJava\\drivers\\chromedriver99.exe"); driver=new ChromeDriver(); //To maximize the size of window driver.manage().window().maximize(); //Navigate through url driver.get(baseURL); //findElement(By by) command WebElement Element=driver.findElement(By.cssSelector("input[id='q']")); //click() command Element.click(); //sendKeys() command Element.sendKeys("Information Technology"); Thread.sleep(3000); //clear() command Element.clear(); Thread.sleep(3000); Element.sendKeys("The Climate and Weather"); //submit() command Element.submit(); driver.close(); }
}