Issue
I am in the process of creating a program for saving Instagram photos from "Saved" area of the page. There are 2 elements:
- "chevrones", which allow you to switch photos inside a multi-publication;
- right arrow", which allow you to move to a next publication.
I want to create a program which goes through all saved photos. If there is a chevron the program should loop through all photos inside this multiple photo publication (click on chevron when it is available). If there is no chevron available when the program should move to the next saved photo (click on the next arrow).
My question is: How to write the proper "IF clause" for 1)firstly loop inside a multiple-publication and 2)then (when all the photos inside multi-pub will finished) go to a next publication.
So far I have following code:
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
}
}
Solution
You can identify an next image >
arrow using the below xpath :
String xpath = "//div[contains(@class, 'RightChevron')]";
If you move to the last image then the above xpath will not return any matches because that image >
arrow is not present for the last/single image.
To check whether the locator is present or not without handling any exceptions, you can use the findElements()
method like below :
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...");
}
If the list size is greater than zero then there is an arrow otherwise not, so the below code will loop through until the size is greater than zero and will click on the image arrow.
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
}
}
Same like an above, you can check for the posts next >
arrow. Below is the whole code which clicks on the image >
arrow if its present or it will click on the next post button until there are some 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
}
}
I hope it helps...
Answered By - Ali
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.