【问题标题】:Is that possible to save pictures with a name from page?是否可以使用页面名称保存图片?
【发布时间】:2021-11-17 03:35:22
【问题描述】:

我想将整个产品的图像保存在一个网站中,它们自己的名字写在同一页面上,是否可以在具有以下逻辑的网站上做到这一点?

主产品页面在同一页面上有整个产品的链接,所以我想我可以在这里获取每个产品,在产品页面中有子菜单,例如“General - Gallery etc”。我想从 General 部分获取产品名称,然后转到 Gallery 部分并使用此名称保存图像,例如 ProductName1.jpg、ProductName2.jpg ...

硒有可能还是不可能?

产品页面:http://www.laboory.com/products

这里是产品的示例链接: http://www.laboory.com/product/laboory-water-soluble-m/3937

【问题讨论】:

    标签: selenium rename


    【解决方案1】:

    是的,我们可以做到这一点。正如你提到的selenium 标签,我假设它使用的是Java

    • 进入产品页面
    • 获取图片来源URL和产品名称。
    • 使用BufferedImageImageIO 类将图像保存到所需位置。

    代码

    driver = new ChromeDriver(options);
    driver.manage().deleteAllCookies();
    
    driver.get("http://laboory.com/product/laboory-water-soluble-m/3937");
                 
    WebElement logo = driver.findElement(By.xpath("(//span//img[@class='imgin' and @src])[1]"));
    String logoSRC = logo.getAttribute("src");
    String productName = driver.findElement(By.xpath("//div/h1")).getText();
         
    URL imageURL = new URL(logoSRC);
    BufferedImage saveImage = ImageIO.read(imageURL);
    ImageIO.write(saveImage, "png", new File(productName+".png"));
    
    

    输出:产品CAPSULE GC 510.png保存在项目目录中。

    注意:您也可以更改位置。

    【讨论】:

      【解决方案2】:

      您可以使用图像元素的尺寸来捕获图像的屏幕截图,并以所需的名称保存,以下是参考 How to capture the screenshot of a specific element rather than entire page using Selenium Webdriver?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-01-25
        • 1970-01-01
        • 2016-02-03
        • 1970-01-01
        • 2014-09-23
        • 2011-07-08
        • 1970-01-01
        • 2015-12-22
        相关资源
        最近更新 更多