I'm trying to write a small app (robot), which will be using a webdriver to log in to the site, search for some data in a search field and then select each element from the list, by clicking one of them, going back to the list and then selecting next element with the same class name but different parent. I think I know how to get all elements with the same class name:
List<WebElement> incognito_user = driver.findElements(By.xpath("//*[@id='results']/li[2]/div/h3/a"))
but I need some help with for loops that will index all of them and also go to next page.
I have solved this issue a bit different way:
boolean inLoop = true;
int maxPages = 2;
while(inLoop && (maxPages-- > 0)) {
List<WebElement> incognito_user = driver.findElements(By.xpath("xpath"));
String openInNewTab = Keys.chord(Keys.CONTROL, Keys.RETURN);
for (WebElement element : incognito_user) {
element.sendKeys(openInNewTab);
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL, Keys.PAGE_DOWN);
waitSecs(randomNumber(5, 10));
driver.switchTo().window(new ArrayList<String>(driver.getWindowHandles()).get(1));
driver.close();
driver.switchTo().window(new ArrayList<String>(driver.getWindowHandles()).get(0));
waitSecs(randomNumber(5,10));
}
List<WebElement> nextElements = driver.findElements(By.xpath("xpath"));
if (nextElements.size() > 0) {
nextElements.get(0).click();
waitSecs(randomNumber(5,10));
}
else {
inLoop = false;
}
}
Thank you for your efort. Kind regards
If u need to deal with list of elements, have just created a wrapper for selenium
click()
public String clickButton(String parentClassIdentifier,String childClassIdentifier ,String index) throws IOException{
WebElement parentButtonList = (WebElement)driver.findElementByXPath((parentClassIdentifier));
List<WebElement> childButtonList = parentButtonList.findElementsByXPath((childClassIdentifier));
int button_index = Integer.parseInt(prop.getProperty(index)); // conversion from String to Int
childButtonList.get(button_index).click();
Application_Log.info("Clicked on button :- " ,childClassIdentifier +index );
return "pass";
}
Simply use clickButton() by passing the identifiers and the list index in the code
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.