Xpath Patterns for Locating WebElements
contains()
Elements can also be located using partial text from an attribute value.
i.e., any attribute, class/id has value: say, 'your class is here'; Here, you can make use of contains for locating elements with partial text, 'your class' truncating 'is here'.
1|
//input[contains(@class, 'your partial class')]
2|
//input[contains(text(),'your partial text')]
not(contains())
'not contains' is just the reverse of 'contains'.
1|
//input[not(contains(@class, 'your class'))]
Excercise |1|
<ul id="subject">
<li class="root">
<a id="abc">"Physics"</a>
</li>
<li class="root">
<a id="abcd">"Chemistry"</a>
</li>
<li class="root">
<a id="abcde">"Mathematics"</a>
</li>
</ul>
List<WebElement> elements = driver.findElements(By.xpath("//ul[@id='subject']/li[not(contains(.,'Chemistry'))]"));
//driver.findElements(By.xpath("//ul[@id='subject']/li[not(contains(a,'Chemistry'))]")); -Alternate Step
System.out.println(elements.size());
for (int i = 0; i < elements.size(); i++) {
System.out.println(ass.get(i).getText());
}
OUTPUT|
2
Physics
Mathematics
text()
The inner text can be used to find Elements.
1|
//input[text()='your text']
2|
//div[contains(text(), 'your text')]
starts-with()
Finds dynamic elements using start of an Element. If your dynamic ids have the format <input id="text-12345" /> where 12345 is a dynamic number you could use the following XPath: //input[starts-with(@id, 'text-')]
Locate and select Auto suggest on search field for a List item (/li) using WebDriver
The below code is for searching a text automatically from the auto suggest; mainly for a list item.driver.get("http://www.indiabookstore.net");
driver.findElement(By.id("searchBox")).sendKeys("Alche");
Thread.sleep(3000);
List <WebElement> listItems = driver.findElements(By.xpath("/html/body/div[4]/ul/li"));
listItems.get(0).click();
driver.findElement(By.id("searchButton")).click();
Note: We can also repalce the xpath locator,
By.xpath("/html/body/div[4]/ul/li") with By.xpath("//div[4]/ul/li")Thread.sleep(3000);
List <WebElement> listItems = driver.findElements(By.xpath("/html/body/div[4]/ul/li"));
listItems.get(0).click();
driver.findElement(By.id("searchButton")).click();
It's not a better way to use the above xpath locator; meanwhile these locators can be replaced with one of the following options (use csslocators for better solution).
List <WebElement> listItems = driver.findElements(By.xpath("//div[contains(@class,'acResults')]//li"));
List <WebElement> listItems = driver.findElements(By.xpath("//div[@class='acResults']//li"));
List <WebElement> listItems = driver.findElements(By.cssSelector(".acResults li"));
List<WebElement> link = driver.findElement(By.id("Element")).findElements(By.tagName("li"));
get(0) is the first option displayed on searching keywords
get(1) is the second option displayed on searching keywords
Anchor Tag :
HTML
<head>
<body id="data-search" class="hassidebar">
<ul id="material-result-list" style="top: 183px; left: 396.5px; width: 270px; display: block;">
<li>
<a>nitrate/0.2</a>
</li>
</ul>
CODE
Here, we need to click on specific anchor tag.
List<WebElement> listItems = driver.findElement(By.id("material-result-list")).findElements(By.tagName("a"));
listItems.get(2).click();
Here, we need to click on specific anchor tag.
List<WebElement> listItems = driver.findElement(By.id("material-result-list")).findElements(By.tagName("a"));
listItems.get(2).click();
Post a Comment
Post a Comment