使用selenium处理Ajax Auto Suggest Drop List

时间:2017-03-30 06:10:55

标签: java selenium-webdriver

我想使用以下代码

http://busindia.com/的From字段中输入值
driver.findElement(By.id("matchFromPlace")).sendKeys("Udupi");

在输入中输入udupi时会出现一个下方选项列表,我们从中选择udupi

3 个答案:

答案 0 :(得分:0)

请尝试以下代码:

Actions action = new Actions(driver);
action.sendKeys(Keys.DOWN);
action.sendKeys(Keys.ENTER);
action.perform()

答案 1 :(得分:0)

以下代码对我有用:

    Thread.sleep(2000);

    List<WebElement> OptionList = driver.findElements(By.xpath("//ul[contains(@class,'ui-autocomplete')]/li/a"));
    if(OptionList.size()>=1){
        for(int i=0;i<OptionList.size();i++)
        {
            String CurrentOption = OptionList.get(i).getText();
            if(CurrentOption.equalsIgnoreCase("UDUPI")){
                System.out.println("Found the city : "+CurrentOption);
                OptionList.get(i).click();
            }
        }
    }
    else{
        System.out.println("OptionList is empty");
    }

答案 2 :(得分:0)

public class Ajaxcontrol {

public WebDriver driver;


@Test()

public void f () throws Exception { 

    driver.findElement(By.xpath("//input[@title='Search']")).sendKeys("selenium");
    Thread.sleep(3000);
    driver.findElement(By.xpath("//input[@name='btnK']")).click();
    Thread.sleep(3000);
    driver.findElement(By.xpath("//input[@title='Search']")).click();
    Thread.sleep(3000);
    String str=driver.findElement(By.xpath("//ul[@class='erkvQe']")).getText();

            System.out.println(str);

             String[] s= str.split("\n");
                System.out.println(s.length);

                        for(int i=0;i<s.length;i++)
{

                                if(s[i].equalsIgnoreCase("selenium tutorial"))
         {
           driver.findElement(By.xpath("//input[@title='Search']")).clear();
           driver.findElement(By.xpath("//input[@title='Search']")).sendKeys(s[i]);
           Thread.sleep(5000);

           driver.findElement(By.xpath("//button[@type='button']")).click();
           Thread.sleep(5000);
           driver.findElement(By.xpath("//h3[contains(text(),'Selenium Tutorial for Beginners: Learn WebDriver i')]")).click();
         }
}


}


@BeforeTest

public void beforeTest() {

    //System.setProperty("webdriver.chrome.driver","C:\\Selenium DriverFiles\\chromedriver.exe");
    //System.setProperty("webdriver.gecko.driver","C:\\Selenium DriverFiles\\geckodriver-v0.23.0-win32\\geckodriver.exe");
    System.setProperty("webdriver.ie.driver","C:\\Selenium DriverFiles\\IEDriverServer.exe");
    driver = new InternetExplorerDriver();
    //driver = new ChromeDriver();
    //driver = new FirefoxDriver();
    driver.get("https://google.com");
    driver.manage().window().maximize();

}


@AfterTest

public void afterTest() {

    driver.quit();
}
}