我正在创建一个程序,用于从页面的“已保存”区域保存instagram照片。 有2个元素:
我想创建一个程序来保存所有保存的照片。如果有shevron,则程序应在多张照片出版物中循环浏览所有照片(在可用时单击shervor )。如果没有可用的shevron,则程序应移至下一张保存的照片(单击下一个箭头)。
我的问题是:如何为以下各项写适当的“ 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
}
}
答案 0 :(得分:1)
您可以使用下面的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
}
}
希望对您有帮助...