Selenium Essentials: A Comprehensive Guide to Using Syntax and Methods
Oct 24, 2024 3 Min Read 963 Views
(Last Updated)
Selenium is an integral part of automation testing and one cannot exist without the usage of others. In that case, it is important for testers to remember all the Selenium essentials!
To ease that out, we made this article that is nothing but a list of Selenium Essentials that can help you remember everything and can help you recollect when in need! Let us see all about them in this article!
Table of contents
- Selenium Essentials: A List of All Essential Syntax and Methods
- Selenium Essentials: A Comprehensive Guide to Using Syntax and Methods
- Different approaches to creating XPath and CSS Selector
- Important Tagname and their Description
- Conclusion
Selenium Essentials: A List of All Essential Syntax and Methods
Driver Initialization |
WebDriver driver = new ChromeDriver(); |
WebDriver driver = new FirefoxDriver(); |
WebDriver driver = new InternetExplorerDriver(); |
WebDriver driver = new HtmlUnitDriver(); |
Element Locators |
driver.findElement(By.id(“Id Value”)); |
driver.findElement(By.name(“Name Value”)); |
driver.findElement(By.className(“Class Name Value”)); |
driver.findElement(By.linkText(“Link text Value”)); |
driver.findElement(By.partialLinkText(“Partial Text ConstantValue”)); |
driver.findElement(By.tagName(“Tag Name Value”)); |
driver.findElement(By.cssSelector(“CSS Value”)); |
driver.findElement(By.xpath(“Xpath Value”)); |
driver.findElement(new ByAll(By.className(“ElementClass Name”), By.id(“Element Id”), By.name(“Element Name”))) |
Elements Operations |
WebElement element = driver.FindElement(By.ElementLocator(“Value of ElementLocator”)); |
element.click(); |
element.sendKeys(“Input Text”); |
element.clear(); |
element.submit(); |
element.getAttribute(“type”); |
String innerText = element.getText(); |
boolean enabledstatus = element.isEnabled(); |
boolean displayedstatus = element.isDisplayed(); |
boolean selectedstatus = element.isSelected(); |
//Operation on drop down |
Select select = new Select(element); |
select.selectByIndex(Integer Index); |
select.selectByVisibleText(“Text”); |
select.SelectByValue(“Value”); |
select.deselectAll(); |
select.deselectByIndex(Integer Index); |
select.deselectByVisibleText(“Text”); |
select.deselectByValue(“Value”); |
WebElement selectedOptions = select.getOptions(); |
Browser Operations |
String pageTitle = driver.getTitle(); |
String currentURL = getCurrentUrl(); |
String currentPageSource = driver.getPageSource(); |
// Navigation history |
driver.get(“https://www.facebook.com/”); |
driver.manage().window().maximize(); |
driver.navigate().to(“https://www.google.com/”); |
driver.navigate().back(); |
driver.navigate().forward(); |
driver.navigate().refresh(); |
Selenium Essentials: A Comprehensive Guide to Using Syntax and Methods
driver.close(); | |
driver.quit(); | |
// Handle Alert | |
Alert alert = driver.switchTo().alert(); | |
alert.accept(); | |
alert.dismiss(); | |
alert.getText(); | |
alert.sendKeys(“Input Data”); | |
//Handle Cookies | |
Cookie cookie = new Cookie(“cookieName”, “cookieValue”); | |
driver.manage().addCookie(cookie); | |
driver.manage().getCookies(); | |
driver.manage().getCookieNamed(arg0); | |
driver.manage().deleteAllCookies(); | |
driver.manage().deleteCookieNamed(arg0); | |
// Handle frames | |
driver.switchTo().frame(int Frame Index); | |
driver.switchTo().frame(“frameName”); | |
WebElement element = driver.FindElement(By.ElementLocator(“Value of ElementLocator”)); | |
driver.switchTo().frame(element); | |
driver.SwitchTo().defaultContent(); | |
Screenshots Capture | |
TakesScreenshot screenshot =((TakesScreenshot)driver); | |
File srcFile= screenshot.getScreenshotAs(OutputType.FILE); | |
FileHandler.copy(srcFile, destFile); | |
Manage Timeouts | |
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS); | |
welement = wait.until(Syntax: WebDriverWait wait = new WebDriverWait(driver, timeout); ExpectedConditions.elementToBeClickable(locator));welement.click(); | |
Thread.sleep(Long milli-seconds) | |
driver.manage().timeouts().pageLoadTimeout(30,TimeUnit.SECONDS); | |
Scroll Down or Up Web Page | |
JavascriptExecutor js = (JavascriptExecutor)driver; | |
js.executeScript(“window.scrollBy(0,100)”); | |
js.executeScript(“window.scrollTo(0,document.body.scrollHeight)”); | |
WebElement element =driver.FindElement(By.ElementLocator(“Value of Element Locator”)); | |
js. executeScript(“arguments[0].scrollIntoView()”, element); |
Different approaches to creating XPath and CSS Selector
Description | XPath | CSS Selector |
Whole WebPage | html | |
Whole WebPage body | /html/body | body |
image element | //img | img |
Link | //a[@href = ‘url’] | a[href = ‘url’] |
Direct Child | //div/a | div > a |
Id | //tagName[@id=’idValue’] | tagName#idValue |
Class | //tagName[@class=’classValue’] | tagName.Value of Classattribute |
Attribute | //tagname[@attribute-name=’value1′] | tagName[attribute=Value of attribute] |
Multiple Attributes | //input[@type=’submit’ and @name=’btnLogin’] | tagname[attribute1=’val ue1′][attribute2=’value2′] |
Contains | //*[contains(@type,’sub’)] | <HTMLtag><[attribute*=subString]> |
Starts with | //tagname[starts-with(@attribute, ‘Start value’)] | <HTMLtag><[attribute^=prefix of the String]> |
Ends with | //tagname[ends-with(@attribute, ‘End value’)] | <HTMLtag><[attribute$=suffix of the String]> |
Matches | //*[matches(@type,’value’)] | N/A |
First Child | //ul[@id=’list’]/li[1] | ul#list li:first-child |
Last Child | //ul[@id=’list’]/li[last()] | ul#list li:last-child |
nth Child | //ul[@id=’list’]/li[3] | ul#list li:nth-child(3) |
Text Value | //td[text()=‘textname’] | N/A |
Element preceding some sibling | //E2/preceding-sibling::E1 | N/A |
Sibling element immediately preceding | //E/preceding-sibling::*[1] | N/A |
User interface element that is disabled | //E[@disabled] | E:disabled |
Checkbox (or radio button) that is checked | //*[@checked] | *:checked |
Text Value | //td[text()=‘textname’] | N/A |
Important Tagname and their Description
Tag Name | Specification |
<ul> | Defines an unordered list of items. |
<tr> | Defines a row of cells in atable. |
<title> | Represents the title to an HTML document. |
<th> | Used for creating a header of a group of cells in an HTML table. |
<table> | Used to define a table in an HTML document. |
<tbody> | Used for grouping table rows. |
<td> | Used for creates standarddata cell in HTML table. |
<span> | Used to grouping and applying styles to inline elements. |
Defines an unordered list of items. | Used to divide a document into a number of different generic sections. |
<select> | Used to create a drop-down list. |
<menu> | Used to display an unordered list of items/menu of commands. |
<li> | Define a list item either an ordered list or an unordered list. |
<iframe> | Defines an inline frame that embeds external content into a current web document. |
<img> | Used to insert an image into a web document. |
<input> | Define a get information in selected input |
<div> | Define a division part |
<body> | Defines a main section(body) part in an HTML document |
<br /> | Specific a single-line break |
<button> | Specifies a press/push button |
<a> | Specific an anchor (Hyperlink) |
These are all the Selenium essentials that you need to keep in mind whenever you are testing!
In case you want to learn more about Selenium essentials in automation testing, consider enrolling for GUVI’s Selenium Automation Testing Course which teaches you everything from scratch by providing an industry-grade certificate!
Conclusion
In conclusion, mastering the Selenium essentials like syntax and methods is crucial for any automation tester looking to efficiently write and maintain test scripts. This comprehensive list serves as a quick reference to help you navigate the most common Selenium operations, from driver initialization to handling alerts, cookies, frames, and more.
By familiarizing yourself with these essentials, you’ll be better equipped to tackle real-world automation challenges and confidently deliver reliable test results.
Did you enjoy this article?