Best Selenium code snippet using org.openqa.selenium.By.findElements
Source:NLPerfectoWebDriver.java  
...156	}157	/**158	 * @param by159	 * @return160	 * @see org.openqa.selenium.remote.RemoteWebDriver#findElements(org.openqa.selenium.By)161	 */162	@Override163	public List<WebElement> findElements(By by) {164		return webDriver.findElements(by);165	}166	/**167	 * @param by168	 * @return169	 * @see org.openqa.selenium.remote.RemoteWebDriver#findElement(org.openqa.selenium.By)170	 */171	@Override172	public WebElement findElement(By by) {173		return webDriver.findElement(by);174	}175	/**176	 * @param using177	 * @return178	 * @see org.openqa.selenium.remote.RemoteWebDriver#findElementById(java.lang.String)179	 */180	@Override181	public WebElement findElementById(String using) {182		return webDriver.findElement(By.id(using));183	}184	/**185	 * @param using186	 * @return187	 * @see org.openqa.selenium.remote.RemoteWebDriver#findElementsById(java.lang.String)188	 */189	@Override190	public List<WebElement> findElementsById(String using) {191		return webDriver.findElements(By.id(using));192	}193	/**194	 * @param using195	 * @return196	 * @see org.openqa.selenium.remote.RemoteWebDriver#findElementByLinkText(java.lang.String)197	 */198	@Override199	public WebElement findElementByLinkText(String using) {200		return webDriver.findElement(By.linkText(using));201	}202	/**203	 * @param using204	 * @return205	 * @see org.openqa.selenium.remote.RemoteWebDriver#findElementsByLinkText(java.lang.String)206	 */207	@Override208	public List<WebElement> findElementsByLinkText(String using) {209		return webDriver.findElements(By.linkText(using));210	}211	/**212	 * @param using213	 * @return214	 * @see org.openqa.selenium.remote.RemoteWebDriver#findElementByPartialLinkText(java.lang.String)215	 */216	@Override217	public WebElement findElementByPartialLinkText(String using) {218		return webDriver.findElement(By.partialLinkText(using));219	}220	/**221	 * @param using222	 * @return223	 * @see org.openqa.selenium.remote.RemoteWebDriver#findElementsByPartialLinkText(java.lang.String)224	 */225	@Override226	public List<WebElement> findElementsByPartialLinkText(String using) {227		return webDriver.findElements(By.partialLinkText(using));228	}229	/**230	 * @param using231	 * @return232	 * @see org.openqa.selenium.remote.RemoteWebDriver#findElementByTagName(java.lang.String)233	 */234	@Override235	public WebElement findElementByTagName(String using) {236		return webDriver.findElement(By.tagName(using));237	}238	/**239	 * @param using240	 * @return241	 * @see org.openqa.selenium.remote.RemoteWebDriver#findElementsByTagName(java.lang.String)242	 */243	@Override244	public List<WebElement> findElementsByTagName(String using) {245		return webDriver.findElements(By.tagName(using));246	}247	/**248	 * @param using249	 * @return250	 * @see org.openqa.selenium.remote.RemoteWebDriver#findElementByName(java.lang.String)251	 */252	@Override253	public WebElement findElementByName(String using) {254		return webDriver.findElement(By.name(using));255	}256	/**257	 * @param using258	 * @return259	 * @see org.openqa.selenium.remote.RemoteWebDriver#findElementsByName(java.lang.String)260	 */261	@Override262	public List<WebElement> findElementsByName(String using) {263		return webDriver.findElements(By.name(using));264	}265	/**266	 * @param using267	 * @return268	 * @see org.openqa.selenium.remote.RemoteWebDriver#findElementByClassName(java.lang.String)269	 */270	@Override271	public WebElement findElementByClassName(String using) {272		return webDriver.findElement(By.className(using));273	}274	/**275	 * @param using276	 * @return277	 * @see org.openqa.selenium.remote.RemoteWebDriver#findElementsByClassName(java.lang.String)278	 */279	@Override280	public List<WebElement> findElementsByClassName(String using) {281		return webDriver.findElements(By.className(using));282	}283	/**284	 * @param using285	 * @return286	 * @see org.openqa.selenium.remote.RemoteWebDriver#findElementByCssSelector(java.lang.String)287	 */288	@Override289	public WebElement findElementByCssSelector(String using) {290		return webDriver.findElement(By.cssSelector(using));291	}292	/**293	 * @param using294	 * @return295	 * @see org.openqa.selenium.remote.RemoteWebDriver#findElementsByCssSelector(java.lang.String)296	 */297	@Override298	public List<WebElement> findElementsByCssSelector(String using) {299		return webDriver.findElements(By.cssSelector(using));300	}301	/**302	 * @param using303	 * @return304	 * @see org.openqa.selenium.remote.RemoteWebDriver#findElementByXPath(java.lang.String)305	 */306	@Override307	public WebElement findElementByXPath(String using) {308		return webDriver.findElement(By.xpath(using));309	}310	/**311	 * @param using312	 * @return313	 * @see org.openqa.selenium.remote.RemoteWebDriver#findElementsByXPath(java.lang.String)314	 */315	@Override316	public List<WebElement> findElementsByXPath(String using) {317		return webDriver.findElements(By.xpath(using));318	}319	/**320	 * @return321	 * @see org.openqa.selenium.remote.RemoteWebDriver#getPageSource()322	 */323	@Override324	public String getPageSource() {325		return webDriver.getPageSource();326	}327	/**328	 * 329	 * @see org.openqa.selenium.remote.RemoteWebDriver#close()330	 */331	@Override...Source:flipkart_homepage.java  
...44	{45	46	//flipkart_Screenshot.takeSnapShot(driver, "Homepage");47		48	List<WebElement> Menu=driver.findElements(By.xpath(bundle.getString("HeaderMenuxpath")));49	System.out.println("headerMenu list of name" + Menu.size());50	51	for(int i=0;i<Menu.size();i++)52	{53		System.out.println(driver.findElements(By.xpath(bundle.getString("HeaderMenuxpath"))).get(i).getText());54		55		if(driver.findElements(By.xpath(bundle.getString("HeaderMenuxpath"))).get(i).getText().contains("Electronics"))56		{57			/*Stale Element Reference exception: "stale element reference element is not attached to the page document" 58			 * when ever such type of expection if we face then we can use 2 methods listed below:59			 * 1. refresh the page and again try for the same element.60			 *     Driver.navigate().refersh();61			 *     Driver.findElement(By.id(�ur element id�)).click();62			 *     63			 * 2. Sometimes it takes the time to attach element on Dom so you can retry using for loop and try catch.64			 *    which is used in the below.    65		 */66		67		68			for(int k=0;k<2;k++)69			{70				try71				{72					//mouse over on the electornics header menu73					action.moveToElement(driver.findElement(By.xpath(bundle.getString("Electronicsheadermenuxpath")))).perform();74					//display the electronics menu list EML75					List<WebElement> EML= driver.findElements(By.cssSelector(bundle.getString("Electronicsmenulistcss")));76					System.out.println("Electronics menu list is " + EML.size());77					78					for(int j=0;j<EML.size();j++)79					{80						//getting the names of electronics menu list name using "gettext()"81					System.out.println(driver.findElements(By.cssSelector(bundle.getString("Electronicsmenulistcss"))).get(j).getText());82					if(driver.findElements(By.cssSelector(bundle.getString("Electronicsmenulistcss"))).get(j).getText().contains("Mi"))83					{84						driver.findElement(By.cssSelector(bundle.getString("MiElectronicscss"))).click();85						86						87					}88					else89					{90						System.out.println("The element is not found");91					}92						93					}94					95					break;96				}97				catch(Exception e)98				{99					System.out.println(e.getMessage());100				}101			}102		}103	}104		105		106		107}108	109	110	public void flipkart_FeatureBrands() 111	{112		113		flipkart_PagetoLoad.waitForLoad(driver,50);114		115		116		117		WebElement fblist= wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(bundle.getString("FeatureBrandlistxpath")))); 118		119		120		/*WebElement fblist= driver.findElement(By.xpath(bundle.getString("FeatureBrandlistxpath")));*/121		122         System.out.println(fblist.findElements(By.tagName("a")).size());123          124         125          for(int k=0;k<fblist.findElements(By.tagName("a")).size();k++)126          {127        	128        	  System.out.println(fblist.findElements(By.tagName("a")).get(k).getAttribute("title"));129        	  if(fblist.findElements(By.tagName("a")).get(k).getAttribute("title").equals("Whirlpool"))130        	  {131        		 WebDriverWait wait= new WebDriverWait(driver,10); 132        		 wait.until(ExpectedConditions.elementToBeClickable(fblist.findElements(By.tagName("a")).get(k))).click();133        		  break;134       	      }135        	  136        	  else137        	  {138        		 fblist.findElement(By.xpath(bundle.getString("FeatureBrandNextbuttonxpath"))).click();139        		 WebElement element= driver.findElement(By.xpath(bundle.getString("FeatureBrandlistxpath")));140        		 JavascriptExecutor executor = (JavascriptExecutor)driver;141        		 executor.executeScript("arguments[0].click();", element);142       		 // wait.until(ExpectedConditions.elementToBeClickable(fblist.findElements(By.tagName("a")).get(k))).click();143        		  144        	  }145       } 146          147	}         148        	149	150	151	152	153	public void flipkart_RandomOffers() throws InterruptedException154	{155		List<WebElement> RandomOffers= driver.findElements(By.xpath(bundle.getString("Randomofferxpath")));156		157		158		159		System.out.println(RandomOffers.size());160		161		162		for(int i=0;i<RandomOffers.size();i++)163		{164			System.out.println(driver.findElements(By.xpath(bundle.getString("Randomofferxpath"))).get(i).getText());165			String Open=driver.findElement(By.xpath(bundle.getString("Random2ndofferxpath"))).getAttribute("target");166			System.out.println("Link should open in same page or new tab" +" "+ Open);167			168			flipkart_RandomOffers_common ABC= new flipkart_RandomOffers_common(driver);169170			if(Open.equals("_blank"))171			{172				System.out.println("link gonna be open in new tab"+driver.getCurrentUrl());173				174				//to handle window in the new tab, method is created and its been called here.175				176				String prodname=driver.findElement(By.xpath(bundle.getString("Random2ndofferxpath"))).getText();177				System.out.println(prodname);178				179				driver.findElement(By.xpath(bundle.getString("Random2ndofferxpath"))).click();180				ABC.flipkart_Newtab();181								182				System.out.println("new tab"+driver.getCurrentUrl());183				184				ABC.flipkart_Text_SizeisPresent();185		}186			187			else188			{189				System.out.println("link gonna be open in same page :"+driver.getCurrentUrl());190				191				wait.until(ExpectedConditions.elementToBeClickable(By.xpath(bundle.getString("Random2ndofferxpath")))).click();192				//driver.findElement(By.xpath(bundle.getString("Random2ndofferxpath"))).click();193				194				ABC.flipkart_Filters_dropdown();195				ABC.flipkart_Filters_Brand();196				197				198				ABC.flipkart_Newtab();199				200				ABC.flipkart_Text_SizeisPresent();201			}202		   203		}204		205	}206207}208209210211212213/*public void flipkart_footer()214{215	WebElement footerdriver=driver.findElement(By.xpath(bundle.getString("Footersectionxpath")));216	System.out.println(footerdriver.findElements(By.tagName("a")).size());217	218	for(int z=0;z<2;z++)219{220		221	try222	{223		for(int i=1;i<footerdriver.findElements(By.tagName("a")).size();i++)224		{225			footerdriver.findElements(By.tagName("a")).get(i).getText();226			if(footerdriver.findElements(By.tagName("a")).get(i).getText().contains("Shipping"))227			{228				footerdriver.findElements(By.tagName("a")).get(i).click();229			}230		}231		break;232	}233	    catch(Exception e)234	    {235		    System.out.println(e.getMessage());236	    }237}238	239}*/
...Source:SharklineTest.java  
...39  @Test40  public void lOGINCONTAINSEMAIL() {41    driver.get("http://localhost:8080/sharkline/");42    {43      List<WebElement> elements = driver.findElements(By.id("email"));44      assert(elements.size() > 0);45    }46    assertThat(driver.findElement(By.cssSelector("form")).getText(), is("Please enter your email in the field. This is the email you use to log into the system.\\\\nEmail Address:\\\\n\\\\nPlease enter the password you used to create your account. If you forget what your password is, please click the \\\"Forgot Password\\\" button below.\\\\nPassword:"));47  }48  @Test49  public void lOGINCONTAINSHELPBUTTON() {50    driver.get("http://localhost:8080/sharkline/");51    driver.findElement(By.id("emailhelp")).click();52    {53      List<WebElement> elements = driver.findElements(By.id("emailhelp"));54      assert(elements.size() == 0);55    }56    driver.findElement(By.id("passwordhelp")).click();57    driver.findElement(By.id("passwordhelp")).click();58    driver.findElement(By.id("passwordhelp")).click();59    {60      List<WebElement> elements = driver.findElements(By.id("passwordhelp"));61      assert(elements.size() == 0);62    }63    driver.findElement(By.cssSelector("form")).click();64    {65      List<WebElement> elements = driver.findElements(By.cssSelector("button:nth-child(2)"));66      assert(elements.size() > 0);67    }68    driver.findElement(By.cssSelector("button:nth-child(2)")).click();69    driver.findElement(By.id("emailhelp")).click();70    {71      List<WebElement> elements = driver.findElements(By.id("emailhelp"));72      assert(elements.size() > 0);73    }74    driver.findElement(By.id("passwordhelp")).click();75    {76      List<WebElement> elements = driver.findElements(By.id("passwordhelp"));77      assert(elements.size() > 0);78    }79  }80  @Test81  public void lOGINCONTAINSPASSWORD() {82    driver.get("http://localhost:8080/sharkline/");83    assertThat(driver.findElement(By.cssSelector("form")).getText(), is("Please enter your email in the field. This is the email you use to log into the system.\\\\nEmail Address:\\\\n\\\\nPlease enter the password you used to create your account. If you forget what your password is, please click the \\\"Forgot Password\\\" button below.\\\\nPassword:"));84    {85      List<WebElement> elements = driver.findElements(By.id("password"));86      assert(elements.size() > 0);87    }88  }89  @Test90  public void lOGINFORGOTPASSWORDPRESENT() {91    driver.get("http://localhost:8080/sharkline/");92    {93      List<WebElement> elements = driver.findElements(By.id("forgot"));94      assert(elements.size() > 0);95    }96    driver.findElement(By.id("forgot")).click();97    assertThat(driver.getTitle(), is("Forgot password?"));98  }99  @Test100  public void lOGINSUCCESFULBUSINESS() {101    driver.get("http://localhost:8080/sharkline/");102    driver.findElement(By.id("email")).sendKeys("amazonHQ@gmail.com");103    driver.findElement(By.id("password")).sendKeys("bezos420");104    driver.findElement(By.cssSelector(".signin_button")).click();105    assertThat(driver.getTitle(), is("SharkLine - Jeff Bezos"));106  }107  @Test...Source:S2Table.java  
...35public class S2Table extends  GUI_automation_base {36	37	public static  int getNumberOfRowsInTable(String tableId,WebDriver driver) {38		39		return driver.findElement(By.id(tableId)).findElements(By.xpath("//tbody/tr")).size();40	}41	public static int getNumberOfRowsInTable(By by,WebDriver driver) {42		return driver.findElement(by).findElements(By.xpath("//tbody/tr")).size();43	}44	public static int getNumberOfColsInTable(By by,WebDriver driver) {45		List<WebElement> rows = driver.findElement(by).findElements(By.xpath("//tbody/tr"));46		WebElement row = rows.get(0);47		return row.findElements(By.tagName("td")).size();48	}49	public static String getCellText(By by, int rowIndex, int colIndex,WebDriver driver) {50		List<WebElement> rows = driver.findElement(by).findElements(By.xpath("//tbody/tr"));51		WebElement row = rows.get(rowIndex);52		List<WebElement> cols = row.findElements(By.tagName("td"));53		WebElement col = cols.get(colIndex);54		return col.getText();55	}56	public static String getCellValue(By by, int rowIndex, int colIndex,WebDriver driver) {57		List<WebElement> rows = driver.findElement(by).findElements(By.xpath("//tbody/tr"));58		WebElement row = rows.get(rowIndex);59		List<WebElement> cols = row.findElements(By.tagName("td"));60		WebElement col = cols.get(colIndex);61		return col.getAttribute("value");62	}63	public static String getCellInnerHTML(By by, int rowIndex, int colIndex,WebDriver driver) {64		List<WebElement> rows = driver.findElement(by).findElements(By.xpath("//tbody/tr"));65		WebElement row = rows.get(rowIndex);66		List<WebElement> cols = row.findElements(By.tagName("td"));67		WebElement col = cols.get(colIndex);68		System.out.println("HTML: " + col.getAttribute("innerHTML"));69		return col.getAttribute("innerHTML");70	}71	public static int getRowIndexInTable(By by, int colIndex, String query,WebDriver driver) {72		int result = -1;73		List<WebElement> rows = driver.findElement(by).findElements(By.xpath("//tbody/tr"));74		int rowIndex = 0;75		for (WebElement row : rows) {76			if (query.equals(row.findElements(By.tagName("td")).get(colIndex).getText())) {77				result = rowIndex;78				break;79			}80			rowIndex++;81		}82		return result;83	}84	public static WebElement getCellSubElement(By tableBy, int rowIndex, int colIndex, By cellSubElementBy,WebDriver driver) {85		List<WebElement> rows = driver.findElement(tableBy).findElements(By.xpath("//tbody/tr"));86		WebElement row = rows.get(rowIndex);87		List<WebElement> cols = row.findElements(By.tagName("td"));88		return cols.get(colIndex).findElement(cellSubElementBy);89	}90	public static  void clickCell(By by, int rowIndex, int colIndex,WebDriver driver) {91		List<WebElement> rows = driver.findElement(by).findElements(By.xpath("//tbody/tr"));92		WebElement row = rows.get(rowIndex);93		List<WebElement> cols = row.findElements(By.tagName("td"));94		95		WebElement DemoElement=cols.get(colIndex);96		String xpath=CommonFunctions.getAbsoluteXPath(DemoElement, driver);97		System.out.println(xpath);98		99		try {100			cols.get(colIndex).click();101		} catch (Exception e) {102			103			System.err.println("Unable to click to index element cell is hidden or index out of range");104	105		}106	}107}...Source:test_admin_left_panel.java  
...33        driver.findElement(By.name("username")).sendKeys("admin");34        driver.findElement(By.name("password")).sendKeys("admin");35        driver.findElement(By.name("login")).click();36        // Ñзнаем ÑÐ°Ð·Ð¼ÐµÑ ÑпиÑка внеÑнего37        List<WebElement> element_list_1  = driver.findElements(By.cssSelector("ul#box-apps-menu > li#app-"));38        int size_list_1 = element_list_1.size();39        // пÑоÑ
одимÑÑ Ð¿Ð¾ ÑпиÑÐºÑ Ð²Ð½ÐµÑнего40        for (int i = 0; i < size_list_1 ; i++) {41            element_list_1  = driver.findElements(By.cssSelector("ul#box-apps-menu > li#app-"));42            element_list_1.get(i).click();43            // пÑовеÑка налиÑÐ¸Ñ Ð·Ð°Ð³Ð¾Ð»Ð¾Ð²ÐºÐ° Ð´Ð»Ñ Ð²Ð½ÑÑÑенниÑ
 ÑпиÑков44            int count = driver.findElements(By.cssSelector("h1")).size();45            if (count < 1)46                System.out.println("CÑÑаниÑа ÐÐ Ð¸Ð¼ÐµÐµÑ Ð·Ð°Ð³Ð¾Ð»Ð¾Ð²Ð¾Ðº");47            else48                System.out.println("CÑÑаниÑа Ð¸Ð¼ÐµÐµÑ " + count + " заголовок");49            // Ñзнаем ÑÐ°Ð·Ð¼ÐµÑ Ð²Ð½ÑÑÑенного ÑпиÑка50            List<WebElement> element_list_2  = driver.findElements(By.cssSelector("ul#box-apps-menu > li#app- [class=docs] span"));51            int size_list_2 = element_list_2.size();52            // пÑоÑ
одимÑÑ Ð¿Ð¾ внÑÑÑенним ÑпиÑкам53            for (int j = 0; j < size_list_2 ; j++) {54                element_list_2  = driver.findElements(By.cssSelector("ul#box-apps-menu > li#app- [class=docs] span"));55                element_list_2.get(j).click();56                // пÑовеÑка налиÑÐ¸Ñ Ð·Ð°Ð³Ð¾Ð»Ð¾Ð²ÐºÐ° Ð´Ð»Ñ Ð²Ð½ÑÑÑенниÑ
 ÑпиÑков57                int count_2 = driver.findElements(By.cssSelector("h1")).size();58                if (count_2 < 1)59                    System.out.println("CÑÑаниÑа ÐÐ Ð¸Ð¼ÐµÐµÑ Ð·Ð°Ð³Ð¾Ð»Ð¾Ð²Ð¾Ðº");60                else61                    System.out.println("CÑÑаниÑа Ð¸Ð¼ÐµÐµÑ " + count_2 + " заголовок");62            }63        }64    }65    @After66    public void stop() {67        driver.quit();68        driver = null;69    }70}...Source:AddToCartTest.java  
...44  @Test45  public void addToCart() {46    driver.get("https://www.sportivo.rs/");47    driver.manage().window().setSize(new Dimension(1296, 688));48    driver.findElements(By.className("cookieinfo-close")).get(0).click();49    driver.get("https://www.sportivo.rs/muska-obuca/muske-patike");50    driver.findElements(By.className("owl-item")).get(0).click();51    driver.findElements(By.className("radio")).get(0).click();52    driver.findElement(By.id("addToCartMD")).click();53    try {54 		Thread.sleep(5000);55 	} catch (InterruptedException e) {56 		// TODO Auto-generated catch block57 		e.printStackTrace();58 	}59    driver.findElement(By.id("fancybox-close")).click();60    try {61 		Thread.sleep(5000);62 	} catch (InterruptedException e) {63 		// TODO Auto-generated catch block64 		e.printStackTrace();65 	}66    driver.get("https://www.sportivo.rs/zenska-obuca/zenske-patike");67    driver.findElements(By.className("owl-item")).get(0).click();68    driver.findElements(By.className("radio")).get(0).click();69    driver.findElement(By.id("addToCartMD")).click();70    try {71 		Thread.sleep(5000);72 	} catch (InterruptedException e) {73 		// TODO Auto-generated catch block74 		e.printStackTrace();75 	}76    driver.findElement(By.id("fancybox-close")).click();77    try {78 		Thread.sleep(5000);79 	} catch (InterruptedException e) {80 		// TODO Auto-generated catch block81 		e.printStackTrace();82 	}83    84    driver.get("https://www.sportivo.rs/decja-obuca/decje-zimske-cipele");85    driver.findElements(By.className("owl-item")).get(0).click();86    driver.findElements(By.className("radio")).get(0).click();87    driver.findElement(By.id("addToCartMD")).click();88    try {89 		Thread.sleep(5000);90 	} catch (InterruptedException e) {91 		// TODO Auto-generated catch block92 		e.printStackTrace();93 	}94    driver.findElement(By.cssSelector(".showcart")).click();95    try {96 		Thread.sleep(5000);97 	} catch (InterruptedException e) {98 		// TODO Auto-generated catch block99 		e.printStackTrace();100 	}101    assertEquals(driver.findElements(By.className("product")).size(), 3);102 103  }104}
...Source:TestBase.java  
...23        String By = FindByPath.replaceAll(":.*", "");24        String path = FindByPath.replaceAll(".*: ", "").replaceAll("]$", "");25        switch (By) {26            case ("xpath"):27                webElementsSize = driver.findElements(org.openqa.selenium.By.xpath(path));28                break;29            case ("className"):30                webElementsSize = driver.findElements(org.openqa.selenium.By.className(path));31                break;32            case ("cssSelector"):33                webElementsSize = driver.findElements(org.openqa.selenium.By.cssSelector(path));34                break;35            case ("id"):36                webElementsSize = driver.findElements(org.openqa.selenium.By.id(path));37                break;38            case ("linkText"):39                webElementsSize = driver.findElements(org.openqa.selenium.By.linkText(path));40                break;41            case ("name"):42                webElementsSize = driver.findElements(org.openqa.selenium.By.name(path));43                break;44            case ("partialLinkText"):45                webElementsSize = driver.findElements(org.openqa.selenium.By.partialLinkText(path));46                break;47            case ("tagName"):48                webElementsSize = driver.findElements(org.openqa.selenium.By.tagName(path));49                break;50            default:51                webElementsSize = null;52                System.out.println("ERROR switch parametr");53                break;54        }55        return webElementsSize;56    }57    @BeforeTest(alwaysRun = true)58    public void preConditions() {59        WebDriverManager.chromedriver().setup();60        driver = new ChromeDriver();61        // driver.manage().window().setSize(new Dimension(6900, 1000));62        //driver.manage().window().setPosition(new Point(-10, -110));...Source:SearchingEventListener.java  
...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}findElements
Using AI Code Generation
1import org.openqa.selenium.By;2import org.openqa.selenium.WebDriver;3import org.openqa.selenium.WebElement;4import org.openqa.selenium.firefox.FirefoxDriver;5import org.openqa.selenium.support.ui.Select;6import java.util.List;7import java.util.concurrent.TimeUnit;8public class FindElements {9    public static void main(String[] args) throws InterruptedException {10        System.setProperty("webdriver.gecko.driver", "C:\\Users\\Dell\\Desktop\\Selenium\\geckodriver.exe");11        WebDriver driver = new FirefoxDriver();12        driver.manage().window().maximize();13        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);14        int dropdownsize = dropdown.size();15        System.out.println("Total number of dropdown values are: "+dropdownsize);16        for(int i=0; i<dropdownsize; i++){17            String dropdownvalues = dropdown.get(i).getText();18            System.out.println(dropdownvalues);19            dropdown.get(i).click();20        }21        Thread.sleep(5000);22        driver.quit();23    }24}findElements
Using AI Code Generation
1package com.test.automation;2import java.util.List;3import org.openqa.selenium.By;4import org.openqa.selenium.WebDriver;5import org.openqa.selenium.WebElement;6import org.openqa.selenium.firefox.FirefoxDriver;7public class FindElementsDemo {8public static void main(String[] args) {9WebDriver driver = new FirefoxDriver();10driver.manage().window().maximize();11List<WebElement> inputElements = driver.findElements(By.tagName("input"));12System.out.println("Number of input elements found = " + inputElements.size());13for(int i=0; i < inputElements.size(); i++) {14System.out.println("Input element " + i + " name = " + inputElements.get(i).getAttribute("name"));15}16driver.quit();17}18}findElements
Using AI Code Generation
1import org.openqa.selenium.WebDriver;2import org.openqa.selenium.WebElement;3import org.openqa.selenium.chrome.ChromeDriver;4import org.openqa.selenium.By;5import java.util.List;6public class FindElements {7    public static void main(String[] args) {8    	System.setProperty("webdriver.chrome.driver", "C:\\Users\\sukanya\\Downloads\\chromedriver_win32\\chromedriver.exe");9    	WebDriver driver = new ChromeDriver();10        String expectedText = "Software Testing Material - DEMO PAGE";11        String actualText = "";12        driver.get(baseUrl);13        actualText = driver.findElement(By.className("entry-title")).getText();14        if (actualText.contentEquals(expectedText)){15            System.out.println("Test Passed!");16        } else {17            System.out.println("Test Failed");18        }19        driver.close();20        System.exit(0);21    }22}findElements
Using AI Code Generation
1package com.selenium4beginners.java.findelements;2import org.openqa.selenium.By;3import org.openqa.selenium.WebDriver;4import org.openqa.selenium.WebElement;5import org.openqa.selenium.chrome.ChromeDriver;6import java.util.List;7public class FindElements {8    public static void main(String[] args) {9        System.setProperty("webdriver.chrome.driver", "chromedriver.exe");10        WebDriver driver = new ChromeDriver();11        driver.manage().window().maximize();12        List<WebElement> links = driver.findElements(By.tagName("a"));13        int numberOfLinks = links.size();14        System.out.println("Number of links on the page: " + numberOfLinks);15        driver.quit();16    }17}18package com.selenium4beginners.java.findelements;19import org.openqa.selenium.By;20import org.openqa.selenium.WebDriver;21import org.openqa.selenium.WebElement;22import org.openqa.selenium.chrome.ChromeDriver;23import java.util.List;24public class FindElementsByPartialLinkText {25    public static void main(String[] args) {26        System.setProperty("webdriver.chrome.driver", "chromedriver.exe");27        WebDriver driver = new ChromeDriver();28        driver.manage().window().maximize();29        List<WebElement> links = driver.findElements(By.partialLinkText("Selenium"));30        int numberOfLinks = links.size();31        System.out.println("Number of links on the page:findElements
Using AI Code Generation
1package com.automation.selenium;2import java.util.List;3import org.openqa.selenium.By;4import org.openqa.selenium.WebDriver;5import org.openqa.selenium.WebElement;6import org.openqa.selenium.chrome.ChromeDriver;7public class Example4 {8	public static void main(String[] args) {9		System.setProperty("webdriver.chrome.driver", "C:\\Users\\kamal\\Downloads\\chromedriver_win32\\chromedriver.exe");10		WebDriver driver = new ChromeDriver();11		List<WebElement> anchors = driver.findElements(By.tagName("a"));12		for(WebElement anchor : anchors) {13			System.out.println(anchor.getText());14		}15		driver.close();16	}17}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!!
