How to select a dynamic changing value from Google search , AJAX controls, webdriver, keydown, keys


The below program will select 3rd value from the Google Search

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class GoogleThirdSelect
{
    public static void main(String[] args) throws Exception
    {
        FirefoxDriver fd = new FirefoxDriver();         ////firefoxdriver
        fd.get("http://www.google.com");
        Thread.sleep(3000);
        WebElement editbox=fd.findElement(By.name("q"));   /////find element (editbox)
       
        editbox.sendKeys("sele");           ///type sele in the inputbox
        Thread.sleep(2000);
        editbox.sendKeys(Keys.ARROW_DOWN);   ////use Keys arrow down to select 3rd value
        editbox.sendKeys(Keys.ARROW_DOWN);
        editbox.sendKeys(Keys.ARROW_DOWN);
       
        editbox.sendKeys(Keys.ENTER);         /////press button
   
    }
}

Comments