Best Selenium code snippet using org.openqa.selenium.Interface SearchContext.findElement
Source:PageUtils.java
...17 protected WebElement findById(String id) {18 return findById(driver, id);19 }20 protected WebElement findById(SearchContext context, String id) {21 return context.findElement(By.id(id));22 }23 // FIND BY CLASS24 25 protected List<WebElement> findByClass(String className) {26 return findByClass(driver, className);27 }28 protected List<WebElement> findByClass(SearchContext context, String className) {29 return context.findElements(By.className(className));30 }31 // FIND BY CSS QUERY32 33 protected WebElement findOneByCss(String query) {34 return findOneByCss(driver, query);35 }36 protected WebElement findOneByCss(SearchContext context, String query) {37 return context.findElement(By.cssSelector(query));38 }39 protected List<WebElement> findByCss(String query) {40 return findByCss(driver, query);41 }42 protected List<WebElement> findByCss(SearchContext context, String query) {43 return context.findElements(By.cssSelector(query));44 }45 46 // FIND BY NAME47 48 protected WebElement findOneByName(String name) {49 return findOneByName(driver, name);50 }51 protected WebElement findOneByName(SearchContext context, String name) {52 return context.findElement(By.name(name));53 }54 // FIND BY XPATH55 56 protected List<WebElement> findByXPath(String xpath) {57 return findByXPath(driver, xpath);58 }59 60 protected List<WebElement> findByXPath(SearchContext context, String xpath) {61 return context.findElements(By.xpath(xpath));62 }63 // COUNT BY CLASS64 65 protected int countByClass(String className) {66 return countByClass(driver, className);67 }68 69 protected int countByClass(SearchContext context, String className) {70 List<WebElement> list = findByClass(context, className);71 if (list != null) {72 return list.size();73 }74 return 0;75 }...
Source:SuviIdealAutomation.java
...10 WebDriver wd = new ChromeDriver();11 wd.get("http://www.suvideals.ooo/Login.action");12/*13 By userInputTextBoxLocator = By.id("userHandle");14 WebElement userInputBox = wd.findElement(userInputTextBoxLocator);15 userInputBox.sendKeys("xyz@hotmail.com");16 By passwordInputTextBoxLocator = By.id("password");17 WebElement passwordInputTextBox = wd.findElement(passwordInputTextBoxLocator);18 passwordInputTextBox.sendKeys("demo123");19 By loginButtonLocator = By.xpath("//button[text()='LOGIN']");20 WebElement loginButton = wd.findElement(loginButtonLocator);21 loginButton.click();22//Chaining of WebElement: If the element does not have any unique attribute.23//You will use one (Parent) WebElement to (child element)find another webElement...24 */25 /*26 * WebDriver is an interface (sub) and SearchContext (super interface)27 * SearchContext : findElements and findElement WebElement is also an sub28 * interface and extends SearchContext (Super Interface). Chaining of29 * WebElement!!30 *31 *32 *33 *34 */35 By createAccountButtonLocator = By.id("new-account-btn"); //Locator36 WebElement createAccountButton = wd.findElement(createAccountButtonLocator);37 createAccountButton.click();38 By registrationFormLocator = By.id("registration-form"); //Locator39 WebElement registrationForm = wd.findElement(registrationFormLocator);40 By userNameLocator = By.name("userName");41 WebElement userNameTextBox = registrationForm.findElement(userNameLocator);42 userNameTextBox.sendKeys("Arsalan");43 By emailLocator = By.name("email");44 WebElement emailTexBox = registrationForm.findElement(emailLocator);45 emailTexBox.sendKeys("abc@hotmail.com");46 By passwordLocator = By.id("password");47 WebElement passwordTextBox = registrationForm.findElement(passwordLocator);48 passwordTextBox.sendKeys("125qwx");49 By createNewAccountButtonLocator = By.xpath("//button[text()='CREATE NEW ACCOUNT']");50 WebElement createNewAccount = registrationForm.findElement(createNewAccountButtonLocator);51 createNewAccount.click();52 }53}...
Source:Page.java
...42 * 43 * @return44 */45 public SearchContext loadApp() {46 this.setRootApp(SeleniumUtils.expand_shadow_element(driver.findElement(By.tagName("app-root"))));47 return this.rootApp;48 }49 /**50 * Method to open the menu on home page.51 * 52 * @return53 */54 public SearchContext openMenu() {55 SeleniumUtils.wait(2);56 SearchContext appRoot = loadApp();57 SeleniumUtils.wait(1);58 SearchContext appHeader = SeleniumUtils59 .expand_shadow_element(appRoot.findElement(By.cssSelector("cf-header.hydrated")));60 SearchContext ionIcon = SeleniumUtils61 .expand_shadow_element(appHeader.findElement(By.cssSelector("ion-icon.md")));62 WebElement menu = ionIcon.findElement(By.cssSelector(".ionicon"));63 menu.click();64 SeleniumUtils.wait(1);65 return appHeader;66 }67 /**68 * Method for logout69 */70 public void logout() {71 try {72 SearchContext menuHeader = SeleniumUtils73 .expand_shadow_element(openMenu().findElement(By.cssSelector("cf-header-menu.hydrated")));74 menuHeader.findElement(By.cssSelector("button.menu-item:nth-child(4)")).click();75 SeleniumUtils.wait(1);76 } catch (Exception e) {77 // TODO: handle exception78 }79 }80}...
Source:MyLoginAsInterfacePo.java
...45 private SearchContext searchContext;4647 public Header(WebDriver driver, By by) {48 this.driver = driver;49 this.searchContext = driver.findElement(by);50 }5152 public WebElement getIndexLink() {53 return searchContext.findElement(By.xpath("./a[1]"));54 }55 }56}
...
Source:BS_DropDown.java
...26 driver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);27 28 driver.get("https://www.jquery-az.com/boots/demo.php?ex=63.0_2");29 30 driver.findElement(By.xpath("//button[@title='HTML, CSS']")).click();;31//driver.findElement(By.xpath("//button[contains(@class='multiselect')]")).click();;32 List<WebElement>list=driver.findElements(By.xpath("//ul[contains(@class,'multiselect-container')]//li//a//label"));33 System.out.println(list.size());34 for(int i=0;i<list.size();i++) {35 System.out.println(list.get(i).getText());36 if(list.get(i).getText().contains("Angular")) {37 list.get(i).click();38 break;39 }40 }41 }42}...
Source:BootstrapModal.java
...9 this.searchContext = searchContext;10 }11 @Override12 public void dismiss() {13 searchContext.findElement(By.cssSelector("button.btn-default")).click(); // the cancel button14 }15 @Override16 public void accept() {17 searchContext.findElement(By.cssSelector("button.btn-primary")).click(); // the ok button18 }19 @Override20 public String getText() {21 return searchContext.findElement(By.cssSelector("h4.modal-title")).getText(); // the input22 }23 @Override24 public void sendKeys(String keysToSend) {25 searchContext.findElement(By.cssSelector("input[type='text']")).sendKeys(keysToSend);26 }27 @Override28 public void setCredentials(Credentials credentials) {29 throw new UnsupportedOperationException();30 }31 @Override32 public void authenticateUsing(Credentials credentials) {33 throw new UnsupportedOperationException();34 }35}...
Source:ByTest.java
...8public class ByTest extends MockObjectTestCase {9 public void testShouldUseFindsByNameToLocateElementsByName() {10 final AllDriver driver = mock(AllDriver.class);11 checking(new Expectations() {{12 one(driver).findElementByName("cheese");13 }});14 By by = By.name("cheese");15 by.findElement((SearchContext) driver);16 }17 public void xtestShouldUseXPathToFindByNameIfDriverDoesNotImplementFindsByName() {18 final OnlyXPath driver = mock(OnlyXPath.class);19 checking(new Expectations() {{20 one(driver).findElementByXPath("//*[@name='cheese']");21 }});22 By by = By.name("cheese");23 by.findElement((SearchContext) driver);24 }25 private interface AllDriver extends FindsById, FindsByLinkText, FindsByName, FindsByXPath, SearchContext {26 // Place holder27 }28 private interface OnlyXPath extends FindsByXPath, SearchContext {29 }30}...
Source:IElementFinder.java
...12 * @param locator elements locator13 * @param timeout timeout for search14 * @return list of found elements15 */16 List<WebElement> findElements(By locator, long timeout);17 /**18 * Finds element19 * @param locator elements locator20 * @param timeout timeout for search21 * @throws org.openqa.selenium.NoSuchElementException if element was not found in time in desired state22 * @return found element23 */24 WebElement findElement(By locator, long timeout);25 default List<WebElement> findElements(By by) {26 return findElements(by, getDefaultTimeout());27 }28 default WebElement findElement(By by) {29 return findElement(by, getDefaultTimeout());30 }31 /**32 * @return default timeout for element waiting33 */34 long getDefaultTimeout();35}...
findElement
Using AI Code Generation
1import org.openqa.selenium.By;2import org.openqa.selenium.WebDriver;3import org.openqa.selenium.WebElement;4import org.openqa.selenium.chrome.ChromeDriver;5import org.openqa.selenium.support.ui.ExpectedConditions;6import org.openqa.selenium.support.ui.WebDriverWait;7public class FindElementByClassName {8 public static void main(String[] args) {9 System.setProperty("webdriver.chrome.driver", "C:\\Users\\sudhanshu\\Desktop\\Selenium\\chromedriver_win32\\chromedriver.exe");10 WebDriver driver = new ChromeDriver();11 WebElement searchBox = driver.findElement(By.className("gLFyf"));12 searchBox.sendKeys("Selenium");13 WebElement searchButton = driver.findElement(By.className("gNO89b"));14 searchButton.click();15 WebDriverWait wait = new WebDriverWait(driver, 10);16 wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("g")));17 driver.close();18 }19}20WebDriver Interface: findElement() Method21public WebElement findElement(By by)22Type of By Object Method to create the object By.id() By.className() By.name() By.tagName() By.linkText() By.partialLinkText() By.xpath() By.cssSelector()
findElement
Using AI Code Generation
1package com.testautomationguru.ocular.snapshot;2import org.openqa.selenium.By;3import org.openqa.selenium.WebDriver;4import org.openqa.selenium.WebElement;5import org.openqa.selenium.chrome.ChromeDriver;6import org.testng.annotations.Test;7import com.testautomationguru.ocular.Ocular;8public class FindElementTest {9 public void test(){10 WebDriver driver = new ChromeDriver();11 WebElement element = driver.findElement(By.className("site-logo"));12 Ocular.snapshot()13 .from(element)14 .sample().using(driver)15 .compare();16 driver.quit();17 }18}19package com.testautomationguru.ocular.snapshot;20import org.openqa.selenium.By;21import org.openqa.selenium.WebDriver;22import org.openqa.selenium.WebElement;23import org.openqa.selenium.chrome.ChromeDriver;24import org.testng.annotations.Test;25import com.testautomationguru.ocular.Ocular;26public class FindElementTest {27 public void test(){28 WebDriver driver = new ChromeDriver();29 WebElement element = driver.findElements(By.className("site-logo")).get(0);30 Ocular.snapshot()31 .from(element)32 .sample().using(driver)33 .compare();34 driver.quit();35 }36}
findElement
Using AI Code Generation
1package examples;2import org.openqa.selenium.By;3import org.openqa.selenium.WebDriver;4import org.openqa.selenium.WebElement;5import org.openqa.selenium.chrome.ChromeDriver;6public class FindElementExample {7 public static void main(String[] args) {8 System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");9 WebDriver driver = new ChromeDriver();10 WebElement element = driver.findElement(By.id("lst-ib"));11 element.sendKeys("selenium webdriver");12 element.submit();13 driver.quit();14 }15}
findElement
Using AI Code Generation
1import org.openqa.selenium.By;2import org.openqa.selenium.WebDriver;3import org.openqa.selenium.WebElement;4import org.openqa.selenium.chrome.ChromeDriver;5import org.openqa.selenium.support.ui.Select;6public class FindElement {7 public static void main(String[] args) {8 WebDriver driver = new ChromeDriver();9 WebElement selectDemo = driver.findElement(By.id("select-demo"));
LambdaTest’s Selenium 4 tutorial is covering every aspects of Selenium 4 testing with examples and best practices. Here you will learn basics, such as how to upgrade from Selenium 3 to Selenium 4, to some advanced concepts, such as Relative locators and Selenium Grid 4 for Distributed testing. Also will learn new features of Selenium 4, such as capturing screenshots of specific elements, opening a new tab or window on the browser, and new protocol adoptions.
Upgrading From Selenium 3 To Selenium 4?: In this chapter, learn in detail how to update Selenium 3 to Selenium 4 for Java binding. Also, learn how to upgrade while using different build tools such as Maven or Gradle and get comprehensive guidance for upgrading Selenium.
What’s New In Selenium 4 & What’s Being Deprecated? : Get all information about new implementations in Selenium 4, such as W3S protocol adaption, Optimized Selenium Grid, and Enhanced Selenium IDE. Also, learn what is deprecated for Selenium 4, such as DesiredCapabilites and FindsBy methods, etc.
Selenium 4 With Python: Selenium supports all major languages, such as Python, C#, Ruby, and JavaScript. In this chapter, learn how to install Selenium 4 for Python and the features of Python in Selenium 4, such as Relative locators, Browser manipulation, and Chrom DevTool protocol.
Selenium 4 Is Now W3C Compliant: JSON Wireframe protocol is retiring from Selenium 4, and they are adopting W3C protocol to learn in detail about the advantages and impact of these changes.
How To Use Selenium 4 Relative Locator? : Selenium 4 came with new features such as Relative Locators that allow constructing locators with reference and easily located constructors nearby. Get to know its different use cases with examples.
Selenium Grid 4 Tutorial For Distributed Testing: Selenium Grid 4 allows you to perform tests over different browsers, OS, and device combinations. It also enables parallel execution browser testing, reads up on various features of Selenium Grid 4 and how to download it, and runs a test on Selenium Grid 4 with best practices.
Selenium Video Tutorials: Binge on video tutorials on Selenium by industry experts to get step-by-step direction from automating basic to complex test scenarios with Selenium.
LambdaTest also provides certification for Selenium testing to accelerate your career in Selenium automation testing.
Get 100 minutes of automation test minutes FREE!!