Selenium Tutorials 8A - Locating Web Elements Using Different Techniques - Examples

Published: 04 April 2019
on channel: subbus tech
453
11

In this chapter we learn:
Finding an element by ID (driver.findElement(By.id("firstname"))
Finding an element by Name (driver.findElement(By.name("firstname"))
Finding an element by className (driver.findElement(By.className("beautifultextbox"))
Finding an element by Link Text (driver.findElement(By.linkText("google the top search engine"))
Finding an element by Partial Link Text (driver.findElement(By.partialLinkText("google"))
Finding an element by xpath
Finding an element by CSSSelector
Finding elements when the ID, Name, attributes have spaces
Find an element by id – works
Finding an element by name – works
Finding an element by Class Name – Produces error
Finding element by xpath – works
Finding element by CSS Selector - works

Locating an element by ID:
IF the element has id associated with it, we can find the element by
driver.findElement(By.id(“id value”))
driver is the ChromeDriver or FirefoxDriver or InternetExplorerDriver object we create.
findElement() is a method which is used to locate an element. It takes an argument which is a By object and its locator method.
findElement(By.locator()) is the whole thing that is used to identify an element.
Locator can be id, name, className etc.
Once an element is located then we do operations on those elements like entering some text if it is a text box or textarea or click operations if it is a link or a button.
If the operation is entering some text, then we use sendKeys() method to enter text.
If the operation is a click, then we use click() method to enter text.

driver.findElement(By.id("firstname")).sendKeys("Subbu");
Locating an element by Name:
driver.findElement(By.name("firstname")).sendKeys("Subbu");
Locating an element by ClassName:
driver.findElement(By.className("beautifultextbox")).sendKeys("subbu");
Locating an element by linkText:
driver.findElement(By.linkText("google the top search engine")).click();
Locating an element by Partial Link Text:
driver.findElement(By.partialLinkText("google")).click();
Locating an element by xpath:
driver.findElement(By.xpath("//input[@color='red']")).sendKeys("Subbu");
Locating an element by CssSelector:
driver.findElement(By.cssSelector("input[color='red']")).sendKeys("Subbu");
If we have space in id, name etc., all these methods work except By.className. By.className with an error.


On this page of the site you can watch the video online Selenium Tutorials 8A - Locating Web Elements Using Different Techniques - Examples with a duration of hours minute second in good quality, which was uploaded by the user subbus tech 04 April 2019, share the link with friends and acquaintances, this video has already been watched 453 times on youtube and it was liked by 11 viewers. Enjoy your viewing!