Selenium Webdriver Commands Methods For Freshers & Experienced: When we think about automation and if the requirement is an open-source tool that has all functionalities. Then one name comes to our mind that’s Selenium WebDriver. Because selenium WebDiver is one of the open-source tools which support all major features which have Selenium Webdriver Commands Methods and by using this, we can perform testing any web application. Also, it can be used with different scripting languages like JAVA, PYTHON, C#, and others. But Java is used with selenium widely because of its popularity, and both have a common thing that’s both are open source tools.
So in this Selenium Tutorial post, we want to share with you the commonly used Selenium Webdriver Commands Methods, which are mostly used by any automation testers with the syntax and also a simple example which helps you to understand the basics easily.
Let’s discuss one by one all commonly used Selenium Webdriver Commands Methods with briefly and precisely so that you can make the use of those Selenium Webdriver Commands Methods effectively whenever the requirement comes.
One of the common statement which is familiar to you is the below statement. For the understanding of why we write this statement for that, you can visit our previous post, Selenium WebDiver Hierarchy, and Selenium WebDiver architecture post.
WebDriver driver= new firefoxDriver ();
Selenium Webdriver Commands Methods
get(): When your requirement is open a URL in the web browser, then you can use this method of selenium WebDiver.
package selenium.basics; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class Selenium_Webdriver_Methods_close { public static void main(String[] args) { String FirefoxDriverPath = System.getProperty("user.dir")+"\\drivers\\geckodriver.exe"; //String ChromeDriverPath = System.getProperty("user.dir")+"\\drivers\\chromedriver.exe"; System.setProperty("webdriver.gecko.driver", FirefoxDriverPath); // To Disable the Driver Logs & Store that logs into logs.txt System.setProperty(FirefoxDriver.SystemProperty.DRIVER_USE_MARIONETTE,"true"); System.setProperty(FirefoxDriver.SystemProperty.BROWSER_LOGFILE,"logs.txt"); WebDriver driver=new FirefoxDriver(); driver.get("https://www.google.com"); driver.close(); } }
Navigate.To(URL): By using this method, we can able to load a new web page in the current browser window. When this method executes internally, an HTTP get operation is performed, and during that time period, it will block all other operations until the web page is loaded completely.
driver.navigate.To(URL);
package selenium.basics; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class Selenium_Webdriver_Methods_NavigateTo { public static void main(String[] args) { String FirefoxDriverPath = System.getProperty("user.dir")+"\\drivers\\geckodriver.exe"; //String ChromeDriverPath = System.getProperty("user.dir")+"\\drivers\\chromedriver.exe"; System.setProperty("webdriver.gecko.driver", FirefoxDriverPath); // To Disable the Driver Logs & Store that logs into logs.txt System.setProperty(FirefoxDriver.SystemProperty.DRIVER_USE_MARIONETTE,"true"); System.setProperty(FirefoxDriver.SystemProperty.BROWSER_LOGFILE,"logs.txt"); WebDriver driver=new FirefoxDriver(); driver.navigate().to("https://www.google.com"); driver.close(); } }
Difference Between: Selenium Benefits
getCurrentUrl(): When you want to get the currently open web page URL, then you can use this method, which returns the current opened page URL in the string format.
String openedURL= driver.getCurrentUrl(); System.out.println("Opened URL is:"+openedURL);
package selenium.basics; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class Selenium_Webdriver_Methods_getCurrentUrl { public static void main(String[] args) throws InterruptedException { String FirefoxDriverPath = System.getProperty("user.dir")+"\\drivers\\geckodriver.exe"; //String ChromeDriverPath = System.getProperty("user.dir")+"\\drivers\\chromedriver.exe"; System.setProperty("webdriver.gecko.driver", FirefoxDriverPath); // To Disable the Driver Logs & Store that logs into logs.txt System.setProperty(FirefoxDriver.SystemProperty.DRIVER_USE_MARIONETTE,"true"); System.setProperty(FirefoxDriver.SystemProperty.BROWSER_LOGFILE,"logs.txt"); WebDriver driver=new FirefoxDriver(); driver.navigate().to("https://www.google.com"); Thread.sleep(2000); String Cururl=driver.getCurrentUrl(); System.out.println("The CUrrent URL is: "+Cururl); driver.close(); } }
getPageSource(): By using the getPageSource() method, you can able to get the page source of the opened web page, and this method return in string format.
String openedPagePageSource= driver.getPageSource(); System.out.println("Page source is: \n" +openedPagePageSource );
package selenium.basics; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class Selenium_Webdriver_Methods_getPageSource { public static void main(String[] args) throws InterruptedException { String FirefoxDriverPath = System.getProperty("user.dir")+"\\drivers\\geckodriver.exe"; //String ChromeDriverPath = System.getProperty("user.dir")+"\\drivers\\chromedriver.exe"; System.setProperty("webdriver.gecko.driver", FirefoxDriverPath); // To Disable the Driver Logs & Store that logs into logs.txt System.setProperty(FirefoxDriver.SystemProperty.DRIVER_USE_MARIONETTE,"true"); System.setProperty(FirefoxDriver.SystemProperty.BROWSER_LOGFILE,"logs.txt"); WebDriver driver=new FirefoxDriver(); driver.navigate().to("https://www.google.com"); Thread.sleep(2000); String PGSource=driver.getPageSource(); System.out.println("Page Source Is: "+PGSource); System.out.println("Complete Page Source Is Printed"); driver.close(); } }
getTitle(): You can able to get the current web page <title> tag texts or web page title of the opened web page by using the getTitle() method. getTitle() return the page title in the form of a string.
String pageTitle= driver.getTitle(); System.out.println("Loaded page title: "+pageTitle);
package selenium.basics; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class Selenium_Webdriver_Methods_getTitle { public static void main(String[] args) throws InterruptedException { String FirefoxDriverPath = System.getProperty("user.dir")+"\\drivers\\geckodriver.exe"; //String ChromeDriverPath = System.getProperty("user.dir")+"\\drivers\\chromedriver.exe"; System.setProperty("webdriver.gecko.driver", FirefoxDriverPath); // To Disable the Driver Logs & Store that logs into logs.txt System.setProperty(FirefoxDriver.SystemProperty.DRIVER_USE_MARIONETTE,"true"); System.setProperty(FirefoxDriver.SystemProperty.BROWSER_LOGFILE,"logs.txt"); WebDriver driver=new FirefoxDriver(); driver.navigate().to("https://www.google.com"); Thread.sleep(2000); String PGtitle=driver.getTitle(); System.out.println("Page Source Is: "+PGtitle); driver.close(); } }
Close(): After performing all the operations in the current window at finally, we want to close that. So we can close the active window by calling the close() of selenium WebDiver.
driver.close();
package selenium.basics; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class Selenium_Webdriver_Methods_close { public static void main(String[] args) { String FirefoxDriverPath = System.getProperty("user.dir")+"\\drivers\\geckodriver.exe"; //String ChromeDriverPath = System.getProperty("user.dir")+"\\drivers\\chromedriver.exe"; System.setProperty("webdriver.gecko.driver", FirefoxDriverPath); // To Disable the Driver Logs & Store that logs into logs.txt System.setProperty(FirefoxDriver.SystemProperty.DRIVER_USE_MARIONETTE,"true"); System.setProperty(FirefoxDriver.SystemProperty.BROWSER_LOGFILE,"logs.txt"); WebDriver driver=new FirefoxDriver(); driver.get("https://www.google.com"); driver.close(); } }
quit(): in the previous syntax we can see that by using close() method we are able to close the currently active window but suppose there are 5 windows are opened, and you want to close all opened windows that time you can use the quit() method and it will close all the opened windows irrespective if active or non-active.
driver.quit();
package selenium.basics; import java.util.Set; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class Selenium_Webdriver_Methods_quit { public static void main(String[] args) throws InterruptedException { WebDriver driver; String FirefoxDriverPath = System.getProperty("user.dir")+"\\drivers\\geckodriver.exe"; //String ChromeDriverPath = System.getProperty("user.dir")+"\\drivers\\chromedriver.exe"; System.setProperty("webdriver.gecko.driver", FirefoxDriverPath); // To Disable the Driver Logs & Store that logs into logs.txt System.setProperty(FirefoxDriver.SystemProperty.DRIVER_USE_MARIONETTE,"true"); System.setProperty(FirefoxDriver.SystemProperty.BROWSER_LOGFILE,"logs.txt"); driver=new FirefoxDriver(); driver.get("https://www.google.com"); Thread.sleep(1000); driver=new FirefoxDriver(); driver.navigate().to("https://www.google.com/intl/en-GB/gmail/about/#"); Set<String> size=driver.getWindowHandles(); System.out.println("Total Windows : "+size.size()); driver.quit(); } }
Difference Between: Selenium Webdriver Architecture
findElement(locator): If you want to find an element in the active dom, then you can use findElement() with the locators.
WebElement SoftwareTestingoLogo= driver.findElement(By.xpath("//img[@title='SoftwareTestingo']")); System.out.println(SoftwareTestingoLogo);
package selenium.findElement; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class Selenium_WebDriver_FindElement { public static void main(String[] args) { String FirefoxDriverPath = System.getProperty("user.dir")+"\\drivers\\geckodriver.exe"; //String ChromeDriverPath = System.getProperty("user.dir")+"\\drivers\\chromedriver.exe"; System.setProperty("webdriver.gecko.driver", FirefoxDriverPath); // To Disable the Driver Logs & Store that logs into logs.txt System.setProperty(FirefoxDriver.SystemProperty.DRIVER_USE_MARIONETTE,"true"); System.setProperty(FirefoxDriver.SystemProperty.BROWSER_LOGFILE,"logs.txt"); WebDriver driver=new FirefoxDriver(); driver.navigate().to("https://www.google.com/"); driver.findElement(By.xpath("//a[contains(text(),'Gmail')]")).click(); driver.close(); } }
findElements(locator): Suppose there are multiple similar types of elements that are there, and you want to access those elements, then you can use the findElements() method. it will return a list of all those similar elements in insertion order.
List allLinks= driver.findElements(By.xpath("//a")); System.out.println("Total elements found: "+allLinks.size());
package selenium.findElement; import java.util.List; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; public class Selenium_WebDriver_FindElements { public static void main(String[] args) { String FirefoxDriverPath = System.getProperty("user.dir")+"\\drivers\\geckodriver.exe"; //String ChromeDriverPath = System.getProperty("user.dir")+"\\drivers\\chromedriver.exe"; System.setProperty("webdriver.gecko.driver", FirefoxDriverPath); // To Disable the Driver Logs & Store that logs into logs.txt System.setProperty(FirefoxDriver.SystemProperty.DRIVER_USE_MARIONETTE,"true"); System.setProperty(FirefoxDriver.SystemProperty.BROWSER_LOGFILE,"logs.txt"); WebDriver driver=new FirefoxDriver(); driver.navigate().to("https://www.google.com/"); List<WebElement> li= driver.findElements(By.tagName("a")); System.out.println("Total Number Of Links: "+li.size()); driver.close(); } }
Difference Between: findElement() VS findElements()
fullScreen(): This method will open the application in full screen.
driver.manage().window().fullscreen();
package selenium.basics; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class Selenium_Webdriver_Methods_fullScreen { public static void main(String[] args) throws InterruptedException { String FirefoxDriverPath = System.getProperty("user.dir")+"\\drivers\\geckodriver.exe"; //String ChromeDriverPath = System.getProperty("user.dir")+"\\drivers\\chromedriver.exe"; System.setProperty("webdriver.gecko.driver", FirefoxDriverPath); WebDriver driver=new FirefoxDriver(); driver.get("https://www.google.com"); driver.manage().window().fullscreen(); Thread.sleep(10000); driver.close(); } }
maximize(): This method will maximize the browser.
driver.manage().window().maximize();
package selenium.basics; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class Selenium_Webdriver_Methods_Maximize { public static void main(String[] args) { String FirefoxDriverPath = System.getProperty("user.dir")+"\\drivers\\geckodriver.exe"; //String ChromeDriverPath = System.getProperty("user.dir")+"\\drivers\\chromedriver.exe"; System.setProperty("webdriver.gecko.driver", FirefoxDriverPath); // To Disable the Driver Logs & Store that logs into logs.txt System.setProperty(FirefoxDriver.SystemProperty.DRIVER_USE_MARIONETTE,"true"); System.setProperty(FirefoxDriver.SystemProperty.BROWSER_LOGFILE,"logs.txt"); WebDriver driver=new FirefoxDriver(); driver.manage().window().maximize(); driver.get("https://www.google.com"); driver.close(); } }
Difference Between: fullScreen() VS maximize()
getPosition(): By using this, we can able to know what is the position of the window from the left side in terms of X and Y coordinates. the return type of this method is Point (An instance of Point class).
Point position= driver.manage().window().getPosition(); System.out.println("Position of window is: "+position);
package selenium.basics; import org.openqa.selenium.Point; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class Selenium_Webdriver_Methods_getPosition { public static void main(String[] args) { String FirefoxDriverPath = System.getProperty("user.dir")+"\\drivers\\geckodriver.exe"; //String ChromeDriverPath = System.getProperty("user.dir")+"\\drivers\\chromedriver.exe"; System.setProperty("webdriver.gecko.driver", FirefoxDriverPath); // To Disable the Driver Logs & Store that logs into logs.txt System.setProperty(FirefoxDriver.SystemProperty.DRIVER_USE_MARIONETTE,"true"); System.setProperty(FirefoxDriver.SystemProperty.BROWSER_LOGFILE,"logs.txt"); WebDriver driver=new FirefoxDriver(); Point position= driver.manage().window().getPosition(); System.out.println("Position of window is: "+position); driver.get("https://www.google.com"); driver.close(); } }
getX() and getY(): These two methods helps in finding specific elements specific X and Y coordinates on the window. The getX() will return the position from the left side of the window, and getY() will return the position from the top side of the screen.
driver.manage().window().getPosition().getX(); driver.manage().window().getPosition().getY()
package selenium.basics; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class Selenium_Webdriver_Methods_getX_YCoordinates { public static void main(String[] args) { String FirefoxDriverPath = System.getProperty("user.dir")+"\\drivers\\geckodriver.exe"; //String ChromeDriverPath = System.getProperty("user.dir")+"\\drivers\\chromedriver.exe"; System.setProperty("webdriver.gecko.driver", FirefoxDriverPath); // To Disable the Driver Logs & Store that logs into logs.txt System.setProperty(FirefoxDriver.SystemProperty.DRIVER_USE_MARIONETTE,"true"); System.setProperty(FirefoxDriver.SystemProperty.BROWSER_LOGFILE,"logs.txt"); WebDriver driver=new FirefoxDriver(); int x=driver.manage().window().getPosition().getX(); int y=driver.manage().window().getPosition().getY(); System.out.println("The X & Y Cordinates is: "+ "( "+x+", "+y+")"); driver.get("https://www.google.com"); driver.close(); } }
getSize(): By implementing this method, we can able to get the window size.
Dimension size= driver.manage().window().getSize(); System.out.println("Size of current window:"+size);
package selenium.basics; import org.openqa.selenium.Dimension; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class Selenium_Webdriver_Methods_getSize { public static void main(String[] args) { String FirefoxDriverPath = System.getProperty("user.dir")+"\\drivers\\geckodriver.exe"; //String ChromeDriverPath = System.getProperty("user.dir")+"\\drivers\\chromedriver.exe"; System.setProperty("webdriver.gecko.driver", FirefoxDriverPath); // To Disable the Driver Logs & Store that logs into logs.txt System.setProperty(FirefoxDriver.SystemProperty.DRIVER_USE_MARIONETTE,"true"); System.setProperty(FirefoxDriver.SystemProperty.BROWSER_LOGFILE,"logs.txt"); WebDriver driver=new FirefoxDriver(); Dimension size=driver.manage().window().getSize(); System.out.println("Size of the Current Window: "+size); driver.get("https://www.google.com"); driver.close(); } }
getWidth() and getHeight(): By using these two getWidth() and getHeight() method we can able to get the window height and width of the current window.
System.out.println("Width of window: "+ driver.manage().window().getSize().getWidth()); System.out.println("Height of window"+driver.manage().window().getSize().getHeight());
package selenium.basics; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class Selenium_Webdriver_Methods_getwidth_height { public static void main(String[] args) { String FirefoxDriverPath = System.getProperty("user.dir")+"\\drivers\\geckodriver.exe"; //String ChromeDriverPath = System.getProperty("user.dir")+"\\drivers\\chromedriver.exe"; System.setProperty("webdriver.gecko.driver", FirefoxDriverPath); // To Disable the Driver Logs & Store that logs into logs.txt System.setProperty(FirefoxDriver.SystemProperty.DRIVER_USE_MARIONETTE,"true"); System.setProperty(FirefoxDriver.SystemProperty.BROWSER_LOGFILE,"logs.txt"); WebDriver driver=new FirefoxDriver(); System.out.println("Width of window: "+ driver.manage().window().getSize().getWidth()); System.out.println("Height of window: "+driver.manage().window().getSize().getHeight()); driver.get("https://www.google.com"); driver.close(); } }
setPosition(): With the help of this method, we can able to set the position of the window on the screen. within the setPosition() method, you have to pass the x and Y coordinates.
driver.manage().window().setPosition(new Point(50,200));
package selenium.basics; import org.openqa.selenium.Point; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class Selenium_Webdriver_Methods_setPosition { public static void main(String[] args) { String FirefoxDriverPath = System.getProperty("user.dir")+"\\drivers\\geckodriver.exe"; //String ChromeDriverPath = System.getProperty("user.dir")+"\\drivers\\chromedriver.exe"; System.setProperty("webdriver.gecko.driver", FirefoxDriverPath); // To Disable the Driver Logs & Store that logs into logs.txt System.setProperty(FirefoxDriver.SystemProperty.DRIVER_USE_MARIONETTE,"true"); System.setProperty(FirefoxDriver.SystemProperty.BROWSER_LOGFILE,"logs.txt"); WebDriver driver=new FirefoxDriver(); driver.manage().window().setPosition(new Point(50,200)); driver.get("https://www.google.com"); driver.close(); } }
setSize(): we are using this method to set the size of the window is software testing. The first parameter represents the width, and the second one is for height. you can find the syntax below
driver.manage().window().setSize(new Dimension(300,500));
package selenium.basics; import org.openqa.selenium.Dimension; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class Selenium_Webdriver_Methods_setSize { public static void main(String[] args) { String FirefoxDriverPath = System.getProperty("user.dir")+"\\drivers\\geckodriver.exe"; //String ChromeDriverPath = System.getProperty("user.dir")+"\\drivers\\chromedriver.exe"; System.setProperty("webdriver.gecko.driver", FirefoxDriverPath); // To Disable the Driver Logs & Store that logs into logs.txt System.setProperty(FirefoxDriver.SystemProperty.DRIVER_USE_MARIONETTE,"true"); System.setProperty(FirefoxDriver.SystemProperty.BROWSER_LOGFILE,"logs.txt"); WebDriver driver=new FirefoxDriver(); driver.manage().window().setSize(new Dimension(300,500)); driver.get("https://www.google.com"); driver.close(); } }
deleteAllCookies(): Sometimes, while testing an application, you may face some issues because of the cookies. so we can solve issues by calling the deleteAllCookies() method in our program so that it will delete all the cookies of the specific domain, and after that, we can run our program.
driver.manage().deleteAllCookies();
package selenium.basics; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class Selenium_Webdriver_Methods_deleteCookies { public static void main(String[] args) { String FirefoxDriverPath = System.getProperty("user.dir")+"\\drivers\\geckodriver.exe"; //String ChromeDriverPath = System.getProperty("user.dir")+"\\drivers\\chromedriver.exe"; System.setProperty("webdriver.gecko.driver", FirefoxDriverPath); // To Disable the Driver Logs & Store that logs into logs.txt System.setProperty(FirefoxDriver.SystemProperty.DRIVER_USE_MARIONETTE,"true"); System.setProperty(FirefoxDriver.SystemProperty.BROWSER_LOGFILE,"logs.txt"); WebDriver driver=new FirefoxDriver(); driver.manage().deleteAllCookies(); System.out.println("Cookies Deleted"); driver.get("https://www.google.com"); driver.close(); } }
deleteCookieNamed(): we can also delete some specific cookies which we want to delete by using the deleteCookieNamed() method, and in that method, we have to pass the cookies name, which you want to delete. like this, we can delete the specific cookies as per our requirement.
driver.manage().deleteCookieNamed("Some cookie name");
implicitWait(): Some time some of the elements are required extra time to load, but our script is not waiting for loading that time. so we can stop the script execution for some time using the implicitWait() method. We have written a detailed post about Selenium Wait you can check here.
Timeouts implicitWaitEx= driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
pageLoadTimeout(): Some times, because of the server issues, the page load times increased as compared to the normal page load time. so because of that, we are getting errors for such scenarios to we are using pageLoadTimeout() with the wait time.
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
setScriptTimeout(): By using setScriptTimeout() method we can mentioned the wait time duration for an asynchronous script to finish the execution. If it is not executed within the mentioned time duration, then it will through an error.
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
Back (): if you have opened a web URL using navigate.to() method, and you want to go back to the previous URL. Then, by using this method, we can navigate back to the previous page. We have written a detailed post about the Selenium Navigation command.
driver.navigate().back();
Forward (): Similarly, if you want to go to the next page, then you can use this forward() method along with navigate(). We have written a detailed post about the Selenium Navigation command.
driver.navigate().forward();
Refresh (): With the help of this method, we can able to do the page refresh. We have written a detailed post about the Selenium Navigation command.
driver.navigate().refresh();
switchTo(): at the time of testing an application if you got any alert then you can use this method
driver.switchTo().alert();
defaultContent(): Suppose you are inside a frame, and you want to come out from the frame, then you can use the defaultContent() method.
driver.switchTo().defaultContent();
Frame (int index): Suppose there are multiple frames present, and you want to jump to a specific frame than by using the frame(int index) you can move to the particular frame.
driver.switchTo().frame(2);
Frame (String name/id): If you know the frame name or id, then you can switch from one frame to another frame by using the name or Id of that frame.
driver.switchTo().frame("name or ID");
Frame (WebElement element): Suppose you don’t know the name, id or index of a frame but you can able to get the locator details of the frame then also you can switch from your current frame to that frame by this:
driver.switchTo().frame(" WebElement locator");
parentFrame(): Suppose you are present inside an inner frame, and from that inner frame to you want to switchTo the parentFrame, then that time, you can use this method.
driver.switchTo().parentFrame();
getClass(): This method helps you to find out which browser class or browser you are using
Class className= driver.getClass(); System.out.println(className.getSimpleName());
isEnabled(): With this isEnabled(), you can easily find out that a specific element in DOM is enabled or disabled. if the particular element is enabled, then it returns a boolean value true, and if that specific element is disabled, then it returns a false value.
boolean textBox = driver.findElement(By.xpath("//input[@name='textbox1']")).isEnabled();
Click(): if you to perform some operation like click on an element, then you can use click() method.
driver.findElement(By.name("ok")).click();
submit(): submit is an alternative of click(). When you are finding an element in DOM, and you found a button with an attribute type=submit, then, in that case, you can use submit. In some scenarios where the action is done through elements rather than a button in that place, submit works and click won’t.
driver.findElement(By.xpath("//input[@name='comments']")).submit();
Select (): We mostly see in DOM there is a drop-down are present. To deal with the drop-down scenarios, we are using the select class. we can select a value by using any of these 3 methods like selectByVisibleText(), selectByValue() or selectByIndex().
WebElement mySelectedElement = driver.findElement(By.id("select")); Select dropdown= new Select(mySelectedElement); dropdown.selectByVisibleText("Apple");
getScreenshotAs(): During the execution of the script in some places, you may see that your script is failing, and knowing the reason for failing, you need to take the screenshot of the page. so we can take the screenshot by using the getScreenshotAs() method.
File shot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); FileUtils.copyFile(shot, new File("D:\\ shot1.jpg"));
Still, there are so many methods are there which you are using during your automation, but I try my label best to share a few regular methods, and I hope this post may be Help you. If you found that still there are other regular used methods are there, then you can comment in the comment section, and I try to update those methods also in the list.
Thanks for visiting our post. Its an appeal to you guys kindly share your experience with us by using our contact us page so that we can make this platform more helpful.
Searching Keyword: Top Selenium Webdriver Commands Methods, Advanced Selenium Webdriver Commands Methods, Selenium Webdriver Commands Methods important for an interview purpose.
Leave a Reply