如何制作一个循环遍历 instagram 上所有已保存照片的程序
How to make a program which loops over all saved photos on instagram
我正在创建一个程序来保存页面 "Saved" 区域中的 Instagram 照片。
有 2 个元素:
- "人字形",允许您在多出版物中切换照片;
- 向右箭头”,让您转到下一个出版物。
我想创建一个程序来遍历所有已保存的照片。如果有 shevron,程序应该循环遍历此多张照片出版物中的所有照片(在可用时单击 shevor)。如果程序应移至下一张保存的照片时没有可用的 shevron(单击下一个箭头)。
我的问题是:如何为 1)firstly loop in a multiple-publication 和 2)then(当 multi-pub 中的所有照片完成时)编写 propper "IF clause"转到下一个出版物。
到目前为止我有以下代码:
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 将不会 return 任何匹配项,因为 last/single 图片的 >
箭头不存在。
要在不处理任何异常的情况下检查定位器是否存在,您可以使用如下所示的 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...");
}
如果列表大小大于零,则有一个箭头,否则没有,因此下面的代码将循环直到列表大小大于零,然后单击图像箭头。
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
}
}
与上述相同,您可以检查下一个 post 的 >
箭头。下面是点击图像 >
箭头的完整代码,如果它存在,或者它将点击下一个 post 按钮,直到有一些 posts.
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
}
}
希望对您有所帮助...
我正在创建一个程序来保存页面 "Saved" 区域中的 Instagram 照片。 有 2 个元素:
- "人字形",允许您在多出版物中切换照片;
- 向右箭头”,让您转到下一个出版物。
我想创建一个程序来遍历所有已保存的照片。如果有 shevron,程序应该循环遍历此多张照片出版物中的所有照片(在可用时单击 shevor)。如果程序应移至下一张保存的照片时没有可用的 shevron(单击下一个箭头)。
我的问题是:如何为 1)firstly loop in a multiple-publication 和 2)then(当 multi-pub 中的所有照片完成时)编写 propper "IF clause"转到下一个出版物。
到目前为止我有以下代码:
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 将不会 return 任何匹配项,因为 last/single 图片的 >
箭头不存在。
要在不处理任何异常的情况下检查定位器是否存在,您可以使用如下所示的 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...");
}
如果列表大小大于零,则有一个箭头,否则没有,因此下面的代码将循环直到列表大小大于零,然后单击图像箭头。
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
}
}
与上述相同,您可以检查下一个 post 的 >
箭头。下面是点击图像 >
箭头的完整代码,如果它存在,或者它将点击下一个 post 按钮,直到有一些 posts.
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
}
}
希望对您有所帮助...