Selenium WebDriver commands can be classified as Get commands, Navigation commands, WebElement commands, Action commands, and Result commands.
Get Commands - Get commands are the most commonly used commands to perform the basic operations of WebDriver such as opening a browser, performing a few tasks, and then closing the browser.
Syntax: get(String arg) : void
The get() method takes a string URL as a parameter and returns nothing.
Syntax:
Example -
driver.get("https://www.google.com");
// Or It can be also written as
String URL = "https://www.scientecheasy.com";
driver.get(URL);
Get Current URL - The getCurrentURL() method takes nothing as a parameter and returns URL of the web page currently loaded in the browser.
getCurrentUrl() : String
Syntax:
driver.getCurrentUrl();
driver.getCurrentUrl();
Example -
driver.getCurrentUrl();
// The above code can be also written as
String currentUrl = driver.getCurrentUrl();
// Since the return type of getCurrentUrl() method is String. So, we will store it using variable currentUrl with String data type.
Get Title Command - The getTitle() method takes nothing as a parameter and returns the page title of the currently loaded web page. If the web page has no title, it will return a null String.
getTitle() : String
Syntax:
driver.getTitle();
driver.getTitle();
Example -
driver.getTitle();
// The above code can also be written as
String title = driver.getTitle(); // Return type of getTitle is String.
Get Page Source Command- The getPageSource() method accepts nothing as a parameter and returns the source code of the current web page. This method is used to get the page source code of the currently loaded web page.
Syntax:
driver.getPageSource();
Example -
driver.getPageSource();
// Or
String pageSource = driver.getPageSource();
// Since return type of the this method is String. Therefore, the output must be stored in a variable with datatype String.
Get Text Command - The getText() method accepts nothing as a parameter and returns a string value. This method is used to retrieve the inner text of the specified element.
getText() : String
Syntax:
driver.findElement(By.locatorName(“Specified Element”)).getText();
Example -
String text = driver.findElement(By.id("India")).getText(); // id is name of locator. India is a specified element.
System.out.println(text);
// The above code can also be written as
WebElement element=driver.findElement(By.id("India"));
String text=element.getText(); // Return type of getText method is String.
System.out.println("Inner Text: " +text);
Get Tag Name Command- The getTagName() method takes nothing as a parameter and returns a String value. This method is used to get the tagName of the WebElement returned by the findElement(By) method.
getTagName() : String
Syntax:element.getTagName(); // Here. element is a variable whose return type is WebElement.
Example -
String tagName = driver.findElement(By.id("DownloadButton")).getTagName();
// or
WebElement element = driver.findElement(By.id("DownloadButton"));
String tagName = element.getTagName();
System.out.println(tagName);
Get CSS Value Command- The getCssValue() method accepts nothing as a parameter and returns a String value. This method is used to fetch the value of the CSS property of the given web element when it is invoked.
getCssValue() : String
Syntax:
element.getCssValue();
element.getCssValue();
Get Attribute Command - The getAttribute() method takes the String as a parameter and returns a String value. It is used to fetch or get the value of the attribute of the WebElement.
getAttribute(String Name) : String
Syntax:
element.getAttribute();
element.getAttribute();
Example -
WebElement element = driver.findElement(By.id("DownloadButton"));
String attributeValue = element.getAttribute("id"); //This will return "DownloadButton".
System.out.println(attributeValue);
Get Size Command- The getSize() method does not accept any parameter but returns the size of the element on the page. This method is used to fetch the width and height of the rendered element.
getSize() : int
Syntax:
element.getSize();
element.getSize();
Example -
WebElement element = driver.findElement(By.id("DownloadButton"));
Dimension elementSize = element.getSize();
System.out.println(“Height :” + elementSize.height + ”Width : "+ elementSize.width);
Close Command - The close method takes nothing as a parameter and returns nothing. This method is used to close only the browser window that web driver is currently controlling.
close() : void
Syntax:
driver.close();
driver.close();
Quit Command - The quit() method accepts nothing as a parameter and returns nothing. This method is used to close all windows opened by WebDriver.
quit() : void
Syntax:
driver.quit();
driver.quit();
No comments:
Post a Comment
Note: only a member of this blog may post a comment.