How to fetch all links and click those links one by one using Selenium WebDriver
There is no such a good idea to have following scenario :
for (WebElement element : webDriver.findElements(locator.getBy())){
element.click();
}
Why? Because there is no guarantee that the element.click();
will have no effect on other found elements, so the DOM
may be changed, so hence the StaleElementReferenceException
.
It is better to use the following scenario :
int numberOfElementsFound = getNumberOfElementsFound(locator);
for (int pos = 0; pos < numberOfElementsFound; pos++) {
getElementWithIndex(locator, pos).click();
}
This is better because you will always take the WebElement
refreshed, even the previous click had some effects on it.
EDIT : Example added
public int getNumberOfElementsFound(By by) {
return webDriver.findElements(by).size();
}
public WebElement getElementWithIndex(By by, int pos) {
return webDriver.findElements(by).get(pos);
}
Hope to be enough.
fetch all links under/in a specific class-selenium webdriver (java)
Actually you are using incorrect xpath
to locating pret
,summer
sale
,accessories
, bt lawn'16
, sale
, lookbook
, links try as below :-
List<WebElement> allLinks = driver.findElements(By.cssSelector("a.level0"));
Random random = new Random();
WebElement randomLink = allLinks.get(random.nextInt(allLinks.size()));
randomLink.click();
WebDriver script for clicking all links except one with particular name not working
The issue you're encountering is that when you click a link, you're leaving the page. This makes the collection of elements in your List "stale".
This is an approach that should work for you. Of course, this assumes that all of the links have different link text.
List<WebElement> allLinks = driver.findElements(By.tagName("a"));
String ofLinks[] = new String[allLinks.size()];
for (int i = 0; i < allLinks.size(); i++){
if(!allLinks.get(i).getText().equals("SUPPORT")){
ofLinks[i] = allLinks.get(i).getText();
}
}
for (int i = 0; i < allLinks.size(); i++){
if (ofLinks[i] != null){
driver.findElement(By.LinkText(ofLinks[i])).click();
}
driver.findElement(By.linkText("Home")).click();
}
Related Topics
Java.Lang.Noclassdeffounderror: Com/Fasterxml/Jackson/Databind/Exc/Invaliddefinitionexception
@Valid Annotation Is Not Working as Expected
Map Json String Column of a JPA Entity to Java Object Automatically
Selenium Chromedriver Opens a Blank Page Instead of Url on Linux
How to Count the Rows in Hibernate Query Language
Autowiring Httpservletrequest in Spring Controller
Set Drawable Size Programmatically
Reading a Text File Character by Character into a 2D Array in Java
How to Test If Json Collection Object Is Empty in Java
Adb Cannot Connect to Daemon At Tcp:5037
Update Elements in a Jsonobject
Splitting String With Parentheses
How to Stop the Items Duplication in Recyclerview Android
Keytool Error Bash: Keytool: Command Not Found
Making a Mocked Method Return an Argument That Was Passed to It
Generating Unique Random Numbers Effectively in Java
Default Value in Lombok. How to Init Default With Both Constructor and Builder