Finding components in Selenium WebDriver is performed with the assistance of findElement() and findElements() techniques gave by WebDriver and WebElement class.
- findElement() restores a WebElement object dependent on a predefined search models or winds up tossing a special case in the event that it doesn't discover any component coordinating the pursuit rules.
- findElements() restores a rundown of WebElements coordinating the inquiry models. On the off chance that no components are discovered, it restores an unfilled rundown.
The accompanying table records all the Java linguistic structure for finding components in Selenium WebDriver.
Method | Syntax | Description |
---|---|---|
By ID | driver.findElement(By.id (<element ID>)) | Locates an element using the ID attribute |
By name | driver.findElement(By.name (<element name>)) | Locates an element using the Name attribute |
By class name | driver.findElement(By.className (<element class>)) | Locates an element using the Class attribute |
By tag name | driver.findElement(By.tagName (<htmltagname>)) | Locates an element using the HTML tag |
By link text | driver.findElement(By.linkText (<linktext>)) | Locates a link using link text |
By partial link text | driver.findElement(By.partialLinkText (<linktext>)) | Locates a link using the link's partial text |
By CSS | driver.findElement(By.cssSelector (<css selector>)) | Locates an element using the CSS selector |
By XPath | driver.findElement(By.xpath (<xpath>)) | Locates an element using XPath query |
Locators Usage
Presently let us comprehend the handy use of every one of the locator strategies with the assistance of https://www.calculator.net
By ID
Here an item is gotten to with the assistance of IDs. For this situation, it is the ID of the content box. Qualities are gone into the content box utilizing the sendkeys technique with the assistance of ID(cdensity).
driver.findElement(By.id("cdensity")).sendKeys("10");
By Name
Here an article is gotten to with the assistance of names. For this situation, it is the name of the content box. Qualities are gone into the content box utilizing the sendkeys strategy with the assistance of ID(cdensity).
driver.findElement(By.name("cdensity")).sendKeys("10");
By Class Name
Here an item is gotten to with the assistance of Class Names. For this situation, it is the Class name of the WebElement. The Value can be gotten to with the assistance of the gettext technique.
List<WebElement> byclass = driver.findElements(By.className("smalltext smtb"));
By Tag Name
The DOM Tag Name of a component can be utilized to find that specific component in the WebDriver. It is anything but difficult to deal with tables with the assistance of this strategy. Investigate the accompanying code.
WebElement table = driver.findElement(By.id("calctable"));
List<WebElement> row = table.findElements(By.tagName("tr"));
int rowcount = row.size();
By Link Text
This technique assists with finding a connection component with coordinating obvious content.
driver.findElements(By.linkText("Volume")).click();
By partial link text
driver.findElement(By.partialLinkText("Volume")).click();
By CSS
The CSS is utilized as a technique to distinguish the webobject, anyway NOT all programs support CSS ID.
WebElement loginButton = driver.findElement(By.cssSelector("input.login"));
By XPath
XPath represents XML way language. It is an inquiry language for choosing hubs from a XML archive. XPath depends on the tree portrayal of XML archives and gives the capacity to explore around the tree by choosing hubs utilizing an assortment of rules.
driver.findElement(By.xpath(".//*[@id = 'content']/table[1]/tbody/tr/td/table/tbody/tr[2]/td[1]/input")).sendkeys("100");