Important Selenium Code / syntax below –  Quick Revision for Interview

				
								// invoke the browser
			driver.get("https://www.google.com/");
			
			// Quit the browser
			driver.quit();
			
			//close the browser - 
			//if multiple window are open then 
			//it will close current window only
			driver.close(); 
			
			//Get the title of Page
			driver.getTitle(); 
			
			
			
			//Find Element in Selenium
			By locator = By.xpath("");
			WebElement element  = driver.findElement(locator);
			
			//Find all Element in Selenium with some locator
			List<WebElement> list = driver.findElements(locator);
			int totalelement = list.size();
			
			//Send value in the Webelement ( Generally used for sending value in input box)
			element.sendKeys("some value");
			
			//Get text value present in html
			element.getText();
			
			//Get attribute info - Example - getting value attribute of element
			element.getAttribute("value");
			
			//accpet the alert
			driver.switchTo().alert().accept();
			
			
			//reject the alert
			driver.switchTo().alert().dismiss();
			
			//Get the windowhandle of current window where driver control is present
			String currenthandle = driver.getWindowHandle();
			
			// Get all window tabs - below is used for swiching driver to differnet window
			Set<String> listofhandles = driver.getWindowHandles();
			
			//select dropdown options by visible text
			Select select = new Select(element);
			select.selectByVisibleText("Text");
			
			//select dropdown options by index 
			int index = 0;
			Select select = new Select(element);
			select.selectByIndex(index);
			
			Set<String> allwindows = driver.getWindowHandles();
			Iterator<String> iteratoronset = allwindows.iterator();
			String child_window = iteratoronset.next();
			driver.switchTo().window(child_window);
			
			//Exlicit wait in selenium - return webelement
			int timeout = 20 ;
			WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(timeout));
			return wait.until(ExpectedConditions.visibilityOf(element));
			

			//Fluent wait in selenium - return webelement
			int timeout = 20 ;
			int pollingTime = 2;
			Wait<WebDriver> wait = new FluentWait<WebDriver>(driver).withTimeout(Duration.ofSeconds(timeout))
					.pollingEvery(Duration.ofSeconds(pollingTime)).ignoring(NoSuchElementException.class)
					.ignoring(StaleElementReferenceException.class);

			return wait.until(ExpectedConditions.presenceOfElementLocated(locator));
			
				
			
Scroll to Top