How can I loop through list of WebElements and select one WebElement with a condition?
Use findElement-S method as shown below.
for (WebElement element : elements) {
List<WebElement> mightHaveSomeDiv = element.findElements(By.cssSelector("someDiv"));
if (mightHaveSomeDiv.size() > 0) {
//Can iterate the list if you expect more than one div of type someDiv.
String myText = mightHaveSomeDiv.get(0).getText();
if (myText.contains("Ooga Booga")) {
return element;
}
}
}
how to loop through List<WebElement>
You could do something like...
List<WebElement> links = driver.find elements(By.cssSelector('#tblPriorEventsBody td a'));
Iterator<WebElement> iter = links.iterator();
while(iter.hasNext()) {
WebElement we = iter.next();
if (we.text.equals("whatever") {
we.click;
// do something in else perhaps
}
}
Add Webelement text into list by iterating each element using for loop
Not sure what is ActualAdFormat_List
in your code.
Also there's no need to have parenthesis in xpath, remove them as well.
Please define a list of string
like this :
List<String> ActualAdFormat_List = new ArrayList<String>();
and use it like this :
List<WebElement> ActualAdFormats_Elements = driver.findElements(By.xpath("//h4[text()='Select Ad Format']/..//strong"));
for(WebElement AdFormat : ActualAdFormats_Elements) {
ActualAdFormat_List.add(AdFormat.getText());
}
Make sure that,
//h4[text()='Select Ad Format']/..//strong
represent all 5 web elements in HTMLDOM
.
Steps to check:
Press F12 in Chrome
-> go to element
section -> do a CTRL + F
-> then paste the xpath
and see, if your desired element
is getting highlighted with 1/1
matching node.
For loop for Checking list of items in Selenium WebDriver
@Saifur has explained nicely regarding the issue. So, I will just put the code that will see you through
List <WebElement> listofItems = wd.findElements(By.xpath("//*[starts-with(@id,'result_')]//div//div[1]//div//a//img"));
WebDriverWait wait = new WebDriverWait(wd, 20); //Wait time of 20 seconds
for (int i=1; i<=listofItems.size(); i++)
{
/*Getting the list of items again so that when the page is
navigated back to, then the list of items will be refreshed
again */
listofItems = wd.findElements(By.xpath("//*[starts-with(@id,'result_')]//div//div[1]//div//a//img"));
//Waiting for the element to be visible
//Used (i-1) because the list's item start with 0th index, like in an array
wait.until(ExpectedConditions.visibilityOf(listofItems.get(i-1)));
//Clicking on the first element
listofItems.get(i-1).click();
wd.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
System.out.print(i + " element clicked\t--");
System.out.println("pass");
wd.navigate().back();
}
So, above I have just tweaked your code a bit and have the relevant comments where changes are made and why. Hope this works out for you. :)
Clicking all elements found in List<WebElement>
Make sure elements you try to click is visible. If you don't care about visibility click with javascipt:
JavascriptExecutor js = (JavascriptExecutor) driver;
WebDriverWait wait = new WebDriverWait(driver, 5);
List<WebElement> elements = driver.findElements(By.className("needToClick"));
elements.forEach(e -> {
js.executeScript("arguments[0].click();", e);
wait.until(ExpectedConditions.attributeToBe(e,"class", "dontNeedToClick"));
//if element have not only dontNeedToClick class use attributeContains
//wait.until(ExpectedConditions.attributeContains(e,"class", "dontNeedToClick"));
});
Check if you can click one element and class changes:
driver.findElement(By.className("needToClick")).click();
if not try xpath:
driver.findElement(By.xpath("//a[i[@class='needToClick']]"));
//or
driver.findElement(By.xpath("//a[i[contains(@class,'needToClick')]]"));
Finding text of all WebElements that match a xpath using only one List
this should work for you:
final List<String> texts = webDriver.findElements(By.xpath("...")).stream()
.map(WebElement::getText)
.collect(toList());
Related Topics
Javax.Net.Ssl.Sslexception: Certificate Doesn't Match Any of the Subject Alternative Names
Spring-Boot @Autowired Interface (Crudrepository) in My Service Class => Null Pointer Exception
How to Check If an Integer Is in a Given Range
Keytool Error Bash: Keytool: Command Not Found
How to Call a Function Only After the Previous Function Is Fully Executed in Java
Placing a Jlabel At a Specific X,Y Coordinate on a Jpanel
Java Ssl: How to Disable Hostname Verification
Spring Boot Return 204 No-Content When List Is Empty
Java Streams Sum Values of a List of Maps
Most Efficient Solution for Reading Clob to String, and String to Clob in Java
Java H2 In-Memory Database Error: Table Not Found
How to Return Different Results When Calling the Same Mocked Method
Geting Http Status 400 - Required Multipartfile Parameter 'File' Is Not Present in Spring
Object Cannot Be Converted to Integer Error
How to Avoid Thread.Sleep in Unit Tests
Status Expected:<200> But Was:<404> in Spring Test