Selenium WebDriver在获取数据时遇到问题

时间:2015-01-24 05:20:20

标签: selenium selenium-webdriver

public class FindElementsDemo {

public static void main(String[] args) throws InterruptedException{
     java.util.logging.Logger.getLogger("com.gargoylesoftware.htmlunit").setLevel(java.util.logging.Level.OFF);
java.util.logging.Logger.getLogger("org.apache.http").setLevel(java.util.logging.Level.OFF);
    //WebDriver browser = new FirefoxDriver();
    //WebDriver browser = new HtmlUnitDriver();
    HtmlUnitDriver browser = new HtmlUnitDriver();
    browser.get("http://www.ebay.com/");
    browser.findElement(By.xpath("//select[@id='gh-cat']")).click();
    Thread.sleep(5000);
    List<WebElement> allCategories = browser.findElements(By.xpath("//select[@id='gh-cat']//option"));
    System.out.println(allCategories.size());
    for(WebElement categoryName:allCategories){
    System.out.println(categoryName.getText());
    }
   }
 }

我没有获得所有类别列表大小和值

2 个答案:

答案 0 :(得分:2)

你的所有代码都很好。您遇到此问题是因为您使用了HtmlDriver - 此驱动程序不支持javascript;并且似乎使用javascript动态生成下拉列表。

用FirefoxDriver替换你的Htmldriver,应该没问题。

您可以将PhantomJS用于无头驱动程序。

36
All Categories
Antiques
Art
Baby
Books
Business & Industrial
Cameras & Photo
Cell Phones & Accessories
Clothing, Shoes & Accessories
Coins & Paper Money
Collectibles
Computers/Tablets & Networking
Consumer Electronics
Crafts
Dolls & Bears
DVDs & Movies
eBay Motors
Entertainment Memorabilia
Gift Cards & Coupons
Health & Beauty
Home & Garden
Jewelry & Watches
Music
Musical Instruments & Gear
Pet Supplies
Pottery & Glass
Real Estate
Specialty Services
Sporting Goods
Sports Mem, Cards & Fan Shop
Stamps
Tickets & Experiences
Toys & Hobbies
Travel
Video Games & Consoles
Everything Else

答案 1 :(得分:0)

看来您的Xpath可能有问题或循环有问题,请尝试以下代码:

 public class FindElementsDemo {

  public static void main(String[] args) throws InterruptedException{
 java.util.logging.Logger.getLogger("com.gargoylesoftware.htmlunit").setLevel(java.util.logging.Level.OFF);

  java.util.logging.Logger.getLogger("org.apache.http").setLevel(java.util.logging.Level.OFF);

   WebDriver driver = new FirefoxDriver();        


      driver.get("http://www.ebay.com/");
      driver.findElement(By.xpath("//select[@id='gh-cat']")).click();

      Thread.sleep(5000);

     java.util.List<WebElement> allCategories = browser.findElements(By.xpath("//select[@id='gh-cat']//option"));

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


    for(int i=0;i<=allCategories.size()-1;i=i+1)
      {
          System.out.println(allCategories.get(i).getText());
      }
    }
  }