使用webdriver java的autosuggest文本框

时间:2014-04-20 01:54:14

标签: selenium-webdriver

我使用webdriver版本2.41并且浏览器是Firefox 28.我试图在自动建议文本框的下拉列表中查找元素的列表数量.Ex:在中Google.co.in 页面我正在写Banga来获取班加罗尔的建议。一旦我得到建议列表,然后我想在屏幕上显示所有自动建议的文本。我已经编写了代码,但不知道为什么它不起作用。我对selenium webdriver很新。请帮我。这是我的代码:

    import java.util.List;
    import java.util.concurrent.TimeUnit;

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

    public class test {

       public static void main(String[] args) {

          WebDriver driver = new FirefoxDriver();
          driver.get("http://www.google.co.in");
          driver.findElement(By.id("gbqfq")).sendKeys("Banga");
          driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
          List<WebElement> lstobj =  driver.findElements(By.xpath("//div[@class='gsq_a']/table/tbody/tr/td/span/b"));

          System.out.println(lstobj.size());

          for (int i = 0; i<lstobj.size();i++)
          {
            String p= lstobj.get(i).getText();
            System.out.println(p);
          }

    }

}

3 个答案:

答案 0 :(得分:0)

编辑了您使用的xpath以及检索文本的方式:

import java.util.List;
import java.util.concurrent.TimeUnit;

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

public class test {

   public static void main(String[] args) {

      WebDriver driver = new FirefoxDriver();
      driver.get("http://www.google.co.in");
      driver.findElement(By.id("gbqfq")).sendKeys("Banga");
      driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
      List<WebElement> lstobj =  driver.findElements(By.xpath("//table[@class='gssb_m']/tbody/tr"));

      System.out.println(lstobj.size());

      for (int i = 0; i<lstobj.size();i++)
      {
        String p= lstobj.get(i).findElement(By.xpath("//span")).getText();
        System.out.println(p);
      }

}

答案 1 :(得分:0)

我希望这有助于你..
 import java.util.List;     import java.util.concurrent.TimeUnit;

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

public class google{

   public static void main(String[] args) {

      WebDriver driver = new FirefoxDriver();
      driver.get("http://www.google.co.in");
      driver.findElement(By.id("gbqfq")).sendKeys("Banga");
      driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
      WebElement parent=driver.findElement(By.className("gssb_e"));
      List<WebElement> child =  parent.findElements(By.tagName("div"));

      int size=child.size();
      System.out.println(size);

      for (int i =1; i<=size;i++)
      {
        String p= driver.findElement(By.xpath("//*[@id='gsr']/table/tbody/tr[1]/td[2]/table/tbody/tr["+i+"]/td/div/table/tbody/tr/td[1]")).getText();
        System.out.println(p);
        driver.close();
      }

}
}

答案 2 :(得分:0)

$Classes(MyProj.EFModels.mydb.*^[Context.cs])[$Imports
    export class $Name {
        $Properties[
        public $name: $Type = $Type[$Default];]
    }]