Best Selenium code snippet using org.openqa.selenium.Interface WebElement.findElement
Source:WebDriverEventListener.java
...44 * if an exception is thrown.45 */46 void afterNavigateForward(WebDriver driver);47 /**48 * Called before {@link WebDriver#findElement WebDriver.findElement(...)}, or49 * {@link WebDriver#findElements WebDriver.findElements(...)}, or {@link WebElement#findElement50 * WebElement.findElement(...)}, or {@link WebElement#findElement WebElement.findElements(...)}.51 *52 * @param element will be <code>null</code>, if a find method of <code>WebDriver</code> is called.53 */54 void beforeFindBy(By by, WebElement element, WebDriver driver);55 /**56 * Called after {@link WebDriver#findElement WebDriver.findElement(...)}, or57 * {@link WebDriver#findElements WebDriver.findElements(...)}, or {@link WebElement#findElement58 * WebElement.findElement(...)}, or {@link WebElement#findElement WebElement.findElements(...)}.59 *60 * @param element will be <code>null</code>, if a find method of <code>WebDriver</code> is called.61 */62 void afterFindBy(By by, WebElement element, WebDriver driver);63 /**64 * Called before {@link WebElement#click WebElement.click()}.65 */66 void beforeClickOn(WebElement element, WebDriver driver);67 /**68 * Called after {@link WebElement#click WebElement.click()}. Not called, if an exception is69 * thrown.70 */71 void afterClickOn(WebElement element, WebDriver driver);72 /**...
Source:ExplicitFluentWait.java
...34 35 int count=1;36 while (isVisible(driver,By.xpath("//*[text()='Next']"))) {37 if (isVisible(driver,By.xpath("//*[@class='_4rR01T']"))) {38 List<WebElement> list = driver.findElements(By.xpath("//*[@class='_4rR01T']"));39 for (WebElement ele : list) {40 if (isVisible(driver, By.xpath("//*[@class='_4rR01T']"))) {41 for(int i=0;i<10;i++) {42 System.out.println(count+":"+ele.getText());43 count++;44 break;45 46 }47 }48 }49 }50 Thread.sleep(5000);51 driver.findElement(By.xpath("//*[text()='Next']")).click();52 Thread.sleep(5000);53 }54 55 56 }57 public static boolean isVisible(WebDriver driver, By by) {58 Wait<WebDriver> ft = new FluentWait<WebDriver>(driver).withTimeout(Duration.ofSeconds(60))59 .pollingEvery(Duration.ofSeconds(60)).ignoring(NoSuchElementException.class);60 WebElement ele = ft.until(new Function<WebDriver, WebElement>() {61 @Override62 public WebElement apply(WebDriver driver) {63 return driver.findElement(by);64 }65 });66 return ele.isDisplayed();67 }68}69/*70//Demo 2 Working properly71 72 73import java.time.Duration;74import java.util.concurrent.TimeUnit;75import org.openqa.selenium.By;76import org.openqa.selenium.NoSuchElementException;77import org.openqa.selenium.WebDriver;78import org.openqa.selenium.WebElement;79import org.openqa.selenium.firefox.FirefoxDriver;80import org.openqa.selenium.support.ui.FluentWait; //FluentWait is a Class and it is a part of this package81 82import com.google.common.base.Function;83 84public class ExplicitFluentWait {85 public static void main(String[] args) {86 fluentWaitMethod();87 }88 public static void fluentWaitMethod(){89 System.setProperty("webdriver.gecko.driver",".//lib//geckodriver.exe");90 WebDriver driver = new FirefoxDriver();91 driver.get("http://softwaretestingplace.blogspot.com/2017/02/selenium-fluent-wait.html");92 driver.findElement(By.xpath("//*[@id='post-body-5280210221385817166']/div[1]/button")).click();93 94 95 FluentWait<WebDriver> wait = new FluentWait<WebDriver>(driver)96 .withTimeout(Duration.ofSeconds(30))97 .pollingEvery(Duration.ofSeconds(5))98 .ignoring(NoSuchElementException.class);99 100 WebElement element = wait.until(new Function<WebDriver, WebElement>() {101 public WebElement apply(WebDriver driver) {102 WebElement element = driver.findElement(By.xpath("//*[@id=\"myAnchor\"]"));103 String getTextOnPage = element.getText();104 System.out.println(getTextOnPage);105 if(getTextOnPage.equals("www.SoftwareTestingMaterial.com")){106 System.out.println(getTextOnPage);107 return element;108 }else{109 System.out.println("FluentWait Failed");110 return null;111 }112 }113 });114 }115}*/...
Source:AmazonHomePage.java
...26 driver.get("https://www.amazon.com/");27 System.out.println("home page Title:"+ driver.getTitle());28 driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);29 30 WebElement element = driver.findElement(By.cssSelector("#nav-link-accountList>span:nth-of-type(2)")); // listen to 2017/06/22 class 23 mints video31 element.click();32 System.out.println("sign in page title :" + driver.getTitle());33 34 }35 36 public void tryPrimeTest(){37 38 driver.get("https://www.amazon.com/");39 System.out.println("home page Title:"+ driver.getTitle());40 WebElement element = driver.findElement(By.cssSelector("#nav-link-prime .nav-line-2"));41 42 Actions action = new Actions(driver); //abject will take driver input43 action.moveToElement(element).perform(); // moveToElement will take the mouse control to a particular element//perform perform action44 45 WebDriverWait wait = new WebDriverWait(driver,30);46 element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".prime-button-try>a")));47 element.click();48 System.out.println("home page Title:"+ driver.getTitle());49 //driver.findElement(By.cssSelector(".prime-button-try>a")).click();50 }51 52 public void searchBoxTest() throws InterruptedException{53 driver.get("https://www.amazon.com/");54 WebElement element = driver.findElement(By.id("twotabsearchtextbox"));55 element.click();56 element.sendKeys("w");57 element.sendKeys("a");58 59 Thread.sleep(1000);60 element.sendKeys("t");61 62 //List<WebElement>elements = driver.findElements(By.cssSelector("#suggestions div"));63 64 65 FluentWait<WebDriver> wait = new FluentWait<WebDriver>(driver);66 wait.pollingEvery(5,TimeUnit.SECONDS);67 wait.withTimeout(30,TimeUnit.SECONDS);68 69 //List<WebElement>elements =wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.cssSelector("#suggestions div")));70 //System.out.println("list size:" + elements.size());71 72 WebElement elm = wait.until(function);73 System.out.println("element selected:"+ elm.getText());74 }75 76
...
Source:GoogleExampleIT.java
...34 // not the implementation.35 WebDriver driver = getDriver();36driver.get("http://www.google.com");37driver.manage().window().maximize();38WebElement element = driver.findElement(By.name("q"));39element.sendKeys("Rent apartments near me ");40Thread.sleep(1000);41WebElement matchingResult= driver.findElement(By.xpath(".//div[@class='aajZCb']/ul"));42List<WebElement> listResult= matchingResult.findElements(By.xpath("//li/div/div[@class='sbtc']"));43System.out.println(listResult.size());44//if you want to print matching results45 for(WebElement results: listResult)46 {47 String value= results.getText();48 System.out.println(value);49 } 50 }51 @Test52 public void googleMilkExample() throws Exception {53 54 WebDriver driver = getDriver(); 55 driver.manage().window().maximize();56 57 driver.get("http://35.221.29.8:4200/");58 driver.findElement(By.linkText("List Users")).click();59 driver.findElement(By.linkText("Add User")).click();60 driver.findElement(By.id("name")).click();61 driver.findElement(By.id("name")).sendKeys("automationtest");62 driver.findElement(By.id("email")).click();63 driver.findElement(By.id("email")).sendKeys("automationtest@automationtest.com");64 driver.findElement(By.cssSelector(".btn:nth-child(4)")).click();65 66}}...
Source:SearchingEventListener.java
...19import org.openqa.selenium.WebDriver;20import org.openqa.selenium.WebElement;21public interface SearchingEventListener extends Listener {22 /**23 * Called before {@link org.openqa.selenium.WebDriver#findElement WebDriver.findElement(...)},24 * or25 * {@link org.openqa.selenium.WebDriver#findElements WebDriver.findElements(...)}, or26 * {@link org.openqa.selenium.WebElement#findElement WebElement.findElement(...)}, or27 * {@link org.openqa.selenium.WebElement#findElement WebElement.findElements(...)}.28 *29 * @param element will be <code>null</code>, if a find method of <code>WebDriver</code>30 * is called.31 * @param by locator being used32 * @param driver WebDriver33 */34 void beforeFindBy(By by, WebElement element, WebDriver driver);35 /**36 * Called after {@link org.openqa.selenium.WebDriver#findElement WebDriver.findElement(...)},37 * or38 * {@link org.openqa.selenium.WebDriver#findElements WebDriver.findElements(...)}, or39 * {@link org.openqa.selenium.WebElement#findElement WebElement.findElement(...)}, or40 * {@link org.openqa.selenium.WebElement#findElement WebElement.findElements(...)}.41 *42 * @param element will be <code>null</code>, if a find method of <code>WebDriver</code>43 * is called.44 * @param by locator being used45 * @param driver WebDriver46 */47 void afterFindBy(By by, WebElement element, WebDriver driver);48}
Source:PageHome.java
...21 driver.manage().window().maximize();22 }23 @Test24 public void testeInterface() {25 driver.findElement(By.className("btn-page-user")).click();26 WebElement btncadastrar = new WebDriverWait(driver, 10)27 .until(ExpectedConditions.elementToBeClickable(By.id("cadastrar")));28 btncadastrar.click();29 30 driver.findElement(By.id("btnfechar")).click();31 32 WebElement emlInput = new WebDriverWait(driver, 10)33 .until(ExpectedConditions.elementToBeClickable(By.id("mat-input-0")));34 emlInput.sendKeys("email@email.com.br");35 driver.findElement(By.id("mat-input-1")).sendKeys("123.456.789-00");36 driver.findElement(By.id("mat-input-2")).sendKeys("Nome e Sobrenome");37 driver.findElement(By.id("mat-input-3")).sendKeys("01/01/2010");38 driver.findElement(By.id("mat-input-4")).sendKeys("(14) 2222-2222");39 WebElement btncadastro = driver.findElement(By.cssSelector("button#cadastrar.btn.btn-cadastrar"));40 WebElement btntodoscadastro = driver.findElement(By.cssSelector("button#todoscadastros.btn.btn-cadastrar"));41 try {42 Thread.sleep(1000);43 } catch (InterruptedException e) {44 // TODO Auto-generated catch block45 e.printStackTrace();46 }47 btncadastro.click();48 try {49 Thread.sleep(1000);50 } catch (InterruptedException e) {51 // TODO Auto-generated catch block52 e.printStackTrace();53 }54 btntodoscadastro.click();...
Source:Assignment2ImplementList.java
...21 ChromeDriver driver = new ChromeDriver();22 driver.get("https://www.ajio.com/");23 driver.manage().window().maximize();24 driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));25 driver.findElement(By.name("searchVal")).sendKeys("bags", Keys.ENTER);26 driver.findElement(By.xpath("//label[@for='Men']")).click();27 Thread.sleep(3000);28 driver.findElement(By.xpath("//label[@for='Men - Fashion Bags']")).click();29 Thread.sleep(2000);30 System.out.println(driver.findElement(By.xpath("//*[@class='filter-container']/div/div[@class='length']")).getText());31 32 List<WebElement> brand = driver.findElements(By.xpath("//div[@class='brand']"));33 //Interface<Generic> variable name = finding the element part.34 List<WebElement> name = driver.findElements(By.xpath("//div[@class='name']"));35 36 for (int i = 0; i < brand.size(); i++) {37 WebElement brandcount = brand.get(i);38 String text = brandcount.getText();39 System.out.println("Brands are: " +text);40 }41 42 for (int i = 0; i < name.size(); i++) {4344 WebElement webElement = name.get(i);45 String text2 = webElement.getText();46 System.out.println("Names are:" + text2);47 48 }
...
Source:facebook_username.java
...12 driver.get("https:\\www.facebook.com");13 driver.manage().window().maximize();14 15 //TextBox xpath16 /* driver.findElement(By.xpath("//*[@id=\"email\"]")).sendKeys("priyankakasat7@gmail.com");17 driver.findElement(By.xpath("//*[@id=\"pass\"]")).sendKeys("123430703");18 driver.findElement(By.xpath("//*[@name=\"login\"]")).click(); */19 20 // WebElement username=driver.findElement(By.xpath("//*[@id=\"email\"]")); // WebElement is Return type of findElement & also interface21 // WebElement password=driver.findElement(By.xpath("//*[@id=\"pass\"]"));22 // WebElement login= driver.findElement(By.xpath("//*[@name=\"login\"]"));23 WebElement username=driver.findElement(By.id("email"));24 WebElement password=driver.findElement(By.id("pass"));25 WebElement login= driver.findElement(By.xpath("//*[@name=\"login\"]"));26 WebElement createapage= driver.findElement(By.className("_8esh"));27 28 29 username.sendKeys("priyankakasat7@gmail.com"); 30 password.sendKeys("1234563314");31 login.click();32 createapage.click();33 34 username.clear(); // for clear value35 36 37 38 }39}...
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 FindElementDemo {7public static void main(String[] args) {8System.setProperty("webdriver.chrome.driver", "C:\\Users\\saurabh\\Downloads\\chromedriver_win32\\chromedriver.exe");9WebDriver driver = new ChromeDriver();10driver.manage().window().maximize();11element.click();12element1.sendKeys("
findElement
Using AI Code Generation
1WebDriver driver = new FirefoxDriver();2WebElement element = driver.findElement(By.name("q"));3element.sendKeys("Selenium WebDriver");4element.submit();5WebDriver driver = new FirefoxDriver();6List<WebElement> elements = driver.findElement(By.name("q"));7element.sendKeys("Selenium WebDriver");8element.submit();9WebDriver driver = new FirefoxDriver();10List<WebElement> elements = driver.findElements(By.name("q"));11element.sendKeys("Selenium WebDriver");12element.submit();13WebDriver driver = new FirefoxDriver();14WebElement element = driver.findElement(By.name("q"));15element.sendKeys("Selenium WebDriver");16element.submit();17WebDriver driver = new FirefoxDriver();18List<WebElement> elements = driver.findElements(By.name("q"));19element.sendKeys("Selenium WebDriver");20element.submit();21WebDriver driver = new FirefoxDriver();22driver.get("
findElement
Using AI Code Generation
1WebElement element = driver.findElement(By.id("email"));2String value = element.getAttribute("value");3System.out.println("The value of the element is: " + value);4String text = element.getText();5System.out.println("The text of the element is: " + text);6List<WebElement> elements = driver.findElements(By.id("email"));7int size = elements.size();8System.out.println("The size of the list is: " + size);9String tagName = element.getTagName();10System.out.println("The tag name of the element is: " + tagName);11String cssValue = element.getCssValue("color");12System.out.println("The CSS value of the element is: " + cssValue);
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!!