-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXPathAxisDemo2.java
More file actions
32 lines (24 loc) · 1007 Bytes
/
XPathAxisDemo2.java
File metadata and controls
32 lines (24 loc) · 1007 Bytes
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
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class XPathAxisDemo2 {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "./drivers/chromedriver");
WebDriver driver = new ChromeDriver();
driver.get("http://localhost:8080/workspace/xpath.html");
// outmost
WebElement mystery = driver.findElement(By.xpath("//p[@id='bye']/ancestor::*"));
System.out.println(mystery.getTagName());
// parent
WebElement parent = driver.findElement(By.xpath("//p[@id='bye']/ancestor::*[1]"));
System.out.println(parent.getAttribute("id"));
System.out.println("========");
List<WebElement> ancestors = driver.findElements(By.xpath("//p[@id='bye']/ancestor::*"));
for (WebElement ancestor: ancestors) {
System.out.println(ancestor.getTagName() + ": " + ancestor.getAttribute("id"));
}
driver.quit();
}
}