使用任何给定网站的Selenium在java中查找图像的URL

时间:2017-07-07 22:12:30

标签: java image selenium selenium-webdriver

我是使用Selenium Web Driver的新手,但我目前正在进行一项项目,这样我就需要使用他们的名字从网站保存一个人的图像。我在网上找到的许多解决方案似乎至少在我的情况下都不起作用。我需要帮助的主要问题是从网页中挑出特定的图像。例如,如果我使用链接https://www.squarespace.com/about/team/,我需要能够下载图像,或者至少根据他们的名字获取一个人的图像链接。任何信息都会有所帮助,谢谢!

1 个答案:

答案 0 :(得分:0)

以下是您的问题的答案:

您可以使用以下代码块来检索所有团队成员的名称以及仅基于他们姓名的一个人的图像链接:

    import java.util.ArrayList;
    import java.util.List;

    import org.openqa.selenium.By;
    import org.openqa.selenium.JavascriptExecutor;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.chrome.ChromeOptions;

    public class Q44980374_url_of_an_image 
    {

        public static void main(String[] args) 
        {

            System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
            ChromeOptions options = new ChromeOptions();
            options.addArguments("start-maximized");
            options.addArguments("disable-infobars");
            WebDriver driver=new ChromeDriver(options);
            driver.get("https://www.squarespace.com/about/team/");
            JavascriptExecutor jse = (JavascriptExecutor) driver;
            jse.executeScript("window.scrollBy(0,1000)", "");
            //Get the Name of the Members
            List<WebElement> team_member = driver.findElements(By.xpath("//main[@id='content']//div[@class='team-members']/div[@class='team-member']/div[@class='team-member-text']/h3"));
            List<String> mem_name_list = new ArrayList<String>();
            for (WebElement member:team_member)
            {
                    String memeber_name = member.getAttribute("innerHTML");
                    mem_name_list.add(memeber_name);
            }
            List<WebElement> team_member_images = driver.findElements(By.xpath("//main[@id='content']//div[@class='team-members']/div[@class='team-member']/div[@class='team-member-portrait']/img"));
            List<String> mem_image_list = new ArrayList<String>();
            for (WebElement image_link:team_member_images)
            {
                    String memebr_image = image_link.getAttribute("src");
                    mem_image_list.add(memebr_image);
            }
            for(int i=0;i<team_member.size();i++) 
            {
                System.out.println(mem_name_list.get(i)+"'s image link is : "+mem_image_list.get(i) );
            }
        }

    }

此代码块的输出为:

Anthony Casalena's image link is : https://static1.squarespace.com/static/ta/5134cbefe4b0c6fb04df8065/9025/assets/pages/about/team/executive-team/anthony-casalena-300w.jpg
Nicole Anasenes's image link is : https://static1.squarespace.com/static/ta/5134cbefe4b0c6fb04df8065/9025/assets/pages/about/team/executive-team/nicole-anasenes-300w.jpg
Andrew Bartholomew's image link is : https://static1.squarespace.com/static/ta/5134cbefe4b0c6fb04df8065/9025/assets/pages/about/team/executive-team/andrew-bartholomew-300w.jpg
John Colton's image link is : https://static1.squarespace.com/static/ta/5134cbefe4b0c6fb04df8065/9025/assets/pages/about/team/executive-team/john-colton-300w.jpg
Raphael Fontes's image link is : https://static1.squarespace.com/static/ta/5134cbefe4b0c6fb04df8065/9025/assets/pages/about/team/executive-team/raphael-fontes-300w.jpg
David Lee's image link is : https://static1.squarespace.com/static/ta/5134cbefe4b0c6fb04df8065/9025/assets/pages/about/team/executive-team/david-lee-300w.jpg
Kinjil Mathur's image link is : https://static1.squarespace.com/static/ta/5134cbefe4b0c6fb04df8065/9025/assets/pages/about/team/executive-team/kinjil-mathur-300w.jpg
Roberta Meo's image link is : https://static1.squarespace.com/static/ta/5134cbefe4b0c6fb04df8065/9025/assets/pages/about/team/executive-team/roberta-meo-300w.jpg
Kris Passet's image link is : https://static1.squarespace.com/static/ta/5134cbefe4b0c6fb04df8065/9025/assets/pages/about/team/executive-team/kris-passet-300w.jpg

如果这回答您的问题,请告诉我。