我在创建从页的“拯救”省面积的Instagram照片程序的过程。有2个要素:
我想箱子程序至极去throught所有保存的照片。如果有一个shevron程序应该循环throught这多个照片的出版物中的所有照片(上shervor点击可用时)。如果没有shevron时可用的程序应该移动到下一个保存的照片(点击下箭头)。
我的问题是:如何写1正常“IF条款”)首次环多刊物内,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...");
}
如果列表大小大于零则有一个箭头,否则不能,所以下面的代码将遍历,直到大小大于零,并单击图像箭头。
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
}
}
我希望它可以帮助...