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

Publicado em: 04 Abril 2019
no canal de: 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.


Nesta página do site você pode assistir ao vídeo on-line Selenium Tutorials 8A - Locating Web Elements Using Different Techniques - Examples duração hora minuto segundo em boa qualidade , que foi baixado pelo usuário subbus tech 04 Abril 2019, compartilhe o link com seus amigos e conhecidos, no youtube este vídeo já foi visto 453 vezes e gostou 11 espectadores. Boa visualização!