How To Get List Of Auto Suggestion On Google Search Page
package autoSuggestions;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Google {
public static void main(String[] args) throws Exception {
WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.co.in/");
Thread.sleep(2000);
WebElement autoOptions = driver.findElement(By.id("lst-ib"));
autoOptions.sendKeys("mahatama gandhi");
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.visibilityOfElementLocated(By
.xpath(".//*[@id='sbtc']/div[2]/div[2]/div[1]")));
WebElement autoSuggestName = driver.findElement(By
.xpath("//div[@class='sbdd_b']"));
List<WebElement> autoSuggest = autoSuggestName.findElements(By
.xpath(".//*[contains(@class,'sbsb_c gsfs')]"));
System.out
.println("Size of the AutoSuggets is = " + autoSuggest.size());
// print the auto suggest
for (int i = 0; i < autoSuggest.size(); i++) {
System.out.println(i);
System.out.println(autoSuggest.get(i).getText());
}
autoOptions.sendKeys(Keys.ARROW_DOWN);
autoOptions.sendKeys(Keys.ARROW_DOWN);
autoOptions.sendKeys(Keys.RETURN);
driver.close();
}
}
How To handle a webElement with dynamic xpath.
How To use Drag and drop in selenium from one frame to another frame.
Post a Comment