-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindElements.java
More file actions
61 lines (50 loc) · 1.78 KB
/
FindElements.java
File metadata and controls
61 lines (50 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package basics;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
/**
* Created by jfarrier on 7/11/2016.
*/
public class FindElements {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
WebDriver webdriver = new ChromeDriver();
webdriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
webdriver.get("http://www.google.com");
/*
findElement
Zero Matches : throws NoSuchElementException
One Match : returns WebElement
One+ Matches : returns the first WebElement matching the By clause
*/
webdriver.findElement(By.xpath("//a"));
/*
findElements
Zero Matches : return an empty list
One Match : returns list of one WebElement only
One+ Matches : returns list with all matching WebElements
*/
webdriver.findElements(By.xpath("//a"));
/*
Uses either xpath or CSS selectors to find the element, traversing the DOM.
*/
webdriver.findElement(By.xpath("//a"));
webdriver.findElement(By.cssSelector("a"));
/*
Uses the tag name of the element to find the element
*/
webdriver.findElement(By.tagName("a"));
/*
Uses the HTML element attributes to find the element.
*/
webdriver.findElement(By.id("a"));
webdriver.findElement(By.name("a"));
webdriver.findElement(By.className("a"));
/*
Uses the anchor tag link name to find the element
*/
webdriver.findElement(By.linkText("a"));
webdriver.findElement(By.partialLinkText("a"));
}
}