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.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,
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();
By.xpath("/html/body/div[4]/ul/li") with By.xpath("//div[4]/ul/li")
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>
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();
A few improvements to your example:
ReplyDelete- don't use sleep. If I was manually testing a web site I would not wait for 3 seconds so my automation should not either. I want until the list appears. My automation will wait until the list appears.
My full solution would be:
@Test
public void autoCompleteTest() {
String resultsLocator = "div.acResults";
String resultFourLocator = resultsLocator + ">ul>li:nth-of-type(4)";
driver.get("http://www.indiabookstore.net/");
driver.findElement(By.cssSelector("#searchBox")).sendKeys("Alche");
WebElement results = driver.findElement(By.cssSelector(resultsLocator));
waitForIsDisplayed(results);
WebElement result = driver.findElement(By.cssSelector(resultFourLocator));
System.out.println(result.getText());
result.click();
driver.findElement(By.cssSelector("#searchButton")).click();
}
private void waitForIsDisplayed(WebElement we) {
do {
try { Thread.sleep(50); } catch (InterruptedException e) { }
} while(!we.isDisplayed());
}
Hi Darrell!
DeleteI agree with your point in this.
Instead of waitForIsDisplayed, I use expectedcondition which is similar to your script.
I specifically mentioned in this post:
http://seleniumworks.blogspot.in/2013/05/iframes-finding-presenceofelementlocate.html
Lemme know your thoughts..
-Sams
Hi Prashanth Can u please share the code here
Deletegood one.. it works
ReplyDeleteThank you!
DeleteCheers!!
thanks - http://letusnotifyyou.com/print-selenium-autosuggestion-text/
ReplyDeleteThanks for using the tag names instead of the xpath in the example, this helped me in solving an automation issue related to suggestions.
ReplyDeletemobile app development companies
I like your post.. Great information.Thank you...
ReplyDeleteMobile game development company
How to print the first paragraph from the results?
ReplyDeleteif(times.equalsIgnoreCase(time)) {
String xp3 ="//ul[@class='dropdown-content select-dropdown']/li["+i+"]/span"; //syntex
System.out.println(xp3);
WebElement txt = driver.findElement(By.xpath(xp3));
System.out.println(txt.getAttribute("innerHTML"));
txt.click();
System.out.println("click done");
break;
}
I'm not able to click particular value of dropdown. please suggest me and I tried with index and text also but not
Thank You! For sharing such a great article, It’s been an amazing article. It provides lot’s of information, I really enjoyed to read this, I hope, I will get these kinds of information on a regular basis from your side.
ReplyDeleteApps Development Company in South Africa
Hi, I Read your blog and I feel it is a very wonderful, informative blog .
ReplyDeleteThere are a lot of good information on this blog, I'd like to Share and I think people will get a lot of support from this blog.
Thank you for sharing this informative blog, please keep up and share some unique posts with us in the future
Software testing company in India
ARVR Development Company in India
Technical Support Company in India
Very interesting, good job and thanks for sharing such a good blog. your article is so convincing that I never stop myself to say something about it. You’re doing a great job. Keep it up virtual reality development services in India
ReplyDeletecheap kyrie shoes
ReplyDeletekyrie shoes
travis scott jordan