Java 如何制作一个在instagram上循环保存所有照片的程序

Java 如何制作一个在instagram上循环保存所有照片的程序,java,selenium,instagram,Java,Selenium,Instagram,我正在创建一个程序,用于从页面的“已保存”区域保存instagram照片。 有两个要素: “雪佛龙””,允许您在多出版物内切换照片 右箭头”,允许您移动到下一个出版物 我想用板条箱装一个程序,该程序将浏览所有保存的照片。如果有shevron,该程序应循环浏览此多张照片出版物中的所有照片(如果可用,请单击shervor)。如果没有shevron,则该程序应移到下一张保存的照片(单击下一个箭头) 我的问题是:如何编写propper“IF子句”,1)首先在多个出版物中循环,2)然后(当多个出版物

我正在创建一个程序,用于从页面的“已保存”区域保存instagram照片。 有两个要素:

  • 雪佛龙””,允许您在多出版物内切换照片
  • 右箭头”,允许您移动到下一个出版物

我想用板条箱装一个程序,该程序将浏览所有保存的照片。如果有shevron,该程序应循环浏览此多张照片出版物中的所有照片(如果可用,请单击shervor)。如果没有shevron,则该程序应移到下一张保存的照片(单击下一个箭头)

我的问题是:如何编写propper“IF子句”,1)首先在多个出版物中循环,2)然后(当多个出版物中的所有照片都完成后)转到下一个出版物。

到目前为止,我有以下代码:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import java.util.List;

public class TestSelenium {
    public static void main(String[] args){
        WebElement img;
        String src;
        int i =0;

// Set webdriver option
        System.setProperty("webdriver.chrome.driver", "C:\\Program Files (x86)\\Common Files\\Oracle\\Java\\Webdrivers\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.instagram.com/accounts/login/");
// Set waits
        WebDriverWait wait = new WebDriverWait(driver, 5);

// Write down the login
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("input[name='username']"))).sendKeys("%%MY_INSTA_LOGGIN%%");
// Write down the password
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("input[name='password']"))).sendKeys("%%MY_INSTA_PASSWORD%%");
// Click on the Signin button
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("button[type='submit']"))).click();

// Go to the saved page
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div[class='SKguc']"))).click();
        driver.get("https://www.instagram.com/aleksandrqa/saved/");

// Click on the first element
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div[class='eLAPa']"))).click();

            // Click on the next chevron
            wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("a[class='SWk3c  Zk-Zb coreSpriteRightChevron']"))).click();

            // Click on the next arrow
            wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("a[class='HBoOv coreSpriteRightPaginationArrow']"))).click();

// TODO
// Save all photos URLs

    }
}

您可以使用以下xpath识别下一个图像
箭头:

String xpath = "//div[contains(@class, 'RightChevron')]";
如果移动到最后一个图像,则上述xpath将不会返回任何匹配项,因为最后一个/单个图像不存在该图像
箭头

要在不处理任何异常的情况下检查定位器是否存在,可以使用
findElements()
方法,如下所示:

List<WebElement> imageArrow = driver.findElements(By.xpath(xpath));
if(imageArrow.size() > 0) {
    System.out.println("=> The image arrow is present...");
    // Perform some action here
} else {
    System.out.println("=> The image arrow is not present...");
}

我希望它能有所帮助……

这是一项非常简单的任务,您还没有为我们做过多少帮助您解决错误的工作,在尝试这样做之前,您应该阅读更多的javadocs。我已经回答了最后一个问题,抱歉误解。
boolean isThereAnArrow = true;
while(isThereAnArrow) {
    final String xpath = "//div[contains(@class, 'RightChevron')]";
    List<WebElement> imageArrow = driver.findElements(By.xpath(xpath));
    if(imageArrow.size() > 0) {
        System.out.println("=> The image arrow is present...");
        imageArrow.get(0).click(); // Clicking on the image arrow
    } else {
        System.out.println("=> The image arrow is not present...");
        isThereAnArrow = false; // If there is no match then it will help us to break the loop
    }
boolean isThereNextPostArrow = true;
while(isThereNextPostArrow) {
    // Checks for the next '>' image arrow, if not then will break the loop
    // ---------------------------------------------------------------------------
    boolean isThereAnArrow = true;
    while(isThereAnArrow) {
        final String xpath = "//div[contains(@class, 'RightChevron')]";
        List<WebElement> imageArrow = driver.findElements(By.xpath(xpath));
        if(imageArrow.size() > 0) {
            System.out.println("=> The image arrow is present...");

            // Do something here

            imageArrow.get(0).click(); // Clicking on the image arrow
        } else {
            System.out.println("=> The image arrow is not present...");
            isThereAnArrow = false; // If there is no match then it will help us to break the loop
        }
    }
    // ---------------------------------------------------------------------------
    // Checks for the next '>' post arrow, if not then will break the loop
    List<WebElement> nextPost = driver.findElements(By.xpath("//a[contains(@class, 'PaginationArrow')]"));
    if(nextPost.size() > 0) {
        System.out.println("=> The next post arrow is there...");
        nextPost.get(0).click(); // Clicking on the next post
    } else {
        System.out.println("=> The next post arrow is not there...");
        isThereNextPostArrow = false; // If there is no match then it will help us to break the outer loop
    }
}