Java:如何使用Selenium从Amazon抓取图像?

Java:如何使用Selenium从Amazon抓取图像?,java,selenium,xpath,Java,Selenium,Xpath,我正在尝试使用Selenium WebDriver从Amazon上的这个URL抓取页面左侧的6个图像: 但是,无论我尝试什么都会导致错误。到目前为止,我所尝试的: 我尝试直接使用XPATH抓取图像,然后使用“getAttributes”方法提取src。例如,对于页面上的第一个图像,XPATH是: ./*[@id='a-autoid-2']/span/input 因此,我尝试了以下方法: String path1 = ".//*[@id='a-autoid-2']/span/input";

我正在尝试使用Selenium WebDriver从Amazon上的这个URL抓取页面左侧的6个图像:

但是,无论我尝试什么都会导致错误。到目前为止,我所尝试的:

  • 我尝试直接使用XPATH抓取图像,然后使用“getAttributes”方法提取src。例如,对于页面上的第一个图像,XPATH是:

    ./*[@id='a-autoid-2']/span/input

  • 因此,我尝试了以下方法:

      String path1 = ".//*[@id='a-autoid-2']/span/input";
            String url = "http://www.amazon.com/EasyAcc%C2%AE-10000mAh-Brilliant-Smartphone-Bluetooth/dp/B00H9BEC8E";
            WebDriver driver = new FirefoxDriver();
            driver.get(url);
      WebElement s;
            s = driver.findElement(By.xpath(path1));
            String src;
            src = s.getAttribute("src");
            System.out.println(src);
    
    但我找不到来源

    注意:仅当从某些类型的产品中刮取图像时,才会出现此问题。例如,我可以使用Selenium轻松地从该产品中获取图像:

    这段代码工作完美无瑕。只有在刮擦某些产品时,才似乎没有办法解决

  • 我尝试单击图像,这会导致iframe打开,但我也无法从该iframe中刮取图像,即使在切换到iframe后,使用:

    driver.switchTo().frame(IFRAMEID)

  • 我知道我可以使用“截图”的方法,但我想知道是否有一种方法可以直接抓取图像

    谢谢

    试试这段代码

        String path = "//div[@id='imageBlock_feature_div']//span/img";
    
        String url = "http://rads.stackoverflow.com/amzn/click/0615797806";
        WebDriver driver = new FirefoxDriver();
        driver.get(url);
    
        List<WebElement> srcs;
        srcs = driver.findElements(By.xpath(path));
    
        for(WebElement src : srcs) {
            System.out.println(src.getAttribute("src"));
        }
    
        driver.close();
    
    但是,要获得Amazon图像,我建议您尝试AmazonAPI


    好多了。

    您的解决方案运行得非常完美。所以我想我的问题是我使用了错误的XPATH?我无法使用Firebug在您的答案中找到XPATH。。这是定制的吗?我认为XPath有点复杂。为什么不使用CSS路径呢?实际上我也尝试过CSS,但它不起作用。下面列出的解决方案有效。然而,我正试图弄明白为什么当我将鼠标悬停在图像上时Firebug并没有显示这个XPATH。。
        String path = "//div[@id='imageBlock_feature_div']//span/img";
    
        String url = "http://rads.stackoverflow.com/amzn/click/0615797806";
        WebDriver driver = new FirefoxDriver();
        driver.get(url);
    
        List<WebElement> srcs;
        srcs = driver.findElements(By.xpath(path));
    
        for(WebElement src : srcs) {
            System.out.println(src.getAttribute("src"));
        }
    
        driver.close();
    
    2015-01-23 12:36:14 [main]-[INFO] Opened url: http://rads.stackoverflow.com/amzn/click/B00H9BEC8E
    http://ecx.images-amazon.com/images/I/41cOP3mFX3L._SX38_SY50_CR,0,0,38,50_.jpg
    http://ecx.images-amazon.com/images/I/51YkMhRXqcL._SX38_SY50_CR,0,0,38,50_.jpg
    http://ecx.images-amazon.com/images/I/51nSbXF%2BCTL._SX38_SY50_CR,0,0,38,50_.jpg
    http://ecx.images-amazon.com/images/I/31s%2B31F%2BQmL._SX38_SY50_CR,0,0,38,50_.jpg
    http://ecx.images-amazon.com/images/I/41FmTOJEOOL._SX38_SY50_CR,0,0,38,50_.jpg
    http://ecx.images-amazon.com/images/I/41U6qpLJ07L._SX38_SY50_CR,0,0,38,50_.jpg