forked from naveenanimation20/SeleniumJavaCourse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomXpath.java
More file actions
65 lines (42 loc) · 1.83 KB
/
CustomXpath.java
File metadata and controls
65 lines (42 loc) · 1.83 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
62
63
64
65
package SeleniumSessions;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class CustomXpath {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "/Users/naveenkhunteta/Downloads/chromedriver");
WebDriver driver = new ChromeDriver(); //launch chrome
driver.manage().window().maximize(); //maximize window
driver.manage().deleteAllCookies(); //delete all the cookies
//dynamic wait
driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("http://www.half.ebay.com"); //enter URL
//absolute xpath: not recommended
//*[@id='headersearchbar']/div/div[2]/table/tbody/tr/td[2]/input
//1. performance issue
//2. not reliable
//3. can be changed at any time in future
//driver.findElement(By.xpath("//input[@class='actextbox']")).sendKeys("Java");
//driver.findElement(By.xpath("//input[@name='query']")).sendKeys("Java");
driver.findElement(By.xpath("//input[contains(@class,'actextbox')]")).sendKeys("Java");
//dynamic id: input
//id = test_123
//By.id("test_123")
//starts-with
//id = test_456
//id = test_789
//id = test_test_7890_test
//ends-with
//id = 1234_test_t
//id = 23456_test_t
//id = 6789_test_t
// driver.findElement(By.xpath("//input[contains(@id,'test_')]")).sendKeys("Test");
// driver.findElement(By.xpath("//input[starts-with(@id,'test_')]")).sendKeys("Test");
// driver.findElement(By.xpath("//input[ends-with(@id,'_test_t')]")).sendKeys("Test");
//for links: custom xpath:
//all the links are represented by <a> html tag:
driver.findElement(By.xpath("//a[contains(text(),'My Account')]")).click();
}
}