'Selenium, default values assigned to web elements instead of the ones i am search for
I am sending text to an input field then within the array of elements searching for specific by checking if element text equal street address
As you can see i have Thread sleep which is not the right approach, the reason i am using this is because i am not to sure how to proceed with the current state of the default values.
When i click on input field there are some values by default within the web elements i am locating which means if i remove Thread sleep it will fail due to the fact that the default values will be assigned to the web elements so i am wondering what can i do in such case
Since i know the default values i could potential just find one and wait until it disappears but thinking if there is a better approach to deal with it
public LocatedCarParksMap searchForCarPark(String carPark,String location) {
clickOnSearchInput();
searchCarPark.sendKeys(carPark);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
for (WebElement i : locatedCarParkAddresses) {
if(i.getText().equals(location)){
click(i);
}
}
return new LocatedCarParksMap(driver);
}
Solution 1:[1]
You can look for some loaders that are appearing in between you enter the search text and the search result appearance. You can wait for that loader to vanish. After that, you can look for your search results.
Even if you have/don't have loader, this similar FluentWait approach should work:
public LocatedCarParksMap searchForCarPark(String carPark,String location) {
clickOnSearchInput();
searchCarPark.sendKeys(carPark);
//If loader available, write code here to wait for your loader to vanish. Just a simple invisibilityOf should work.
boolean clickStatus = waitAndClickSearchElement(location);
if(! clickStatus) {
throw new Exception(carPark + " is not available in Search Results");
}
return new LocatedCarParksMap(driver);
}
public boolean waitAndClickSearchElement() {
try {
//Change this timeout of 10 sec according to your need.
WebDriverWait webDriverWait = new WebDriverWait(driver,10);
//Increased the polling time to 2 secs to avoid checking the results frequently.
webDriverWait.pollingEvery(Duration.ofSeconds(2))
//Ignoring these 2 exceptions as they are more likely to occur.
webDriverWait.ignoring(NoSuchElementException.class, StaleElementReferenceException.class);
//This until method keeps checking the text for 10 secs. Once it finds the element, will click on it.
return webDriverWait.until(driver -> {
for (WebElement weAddress : locatedCarParkAddresses) {
if(weAddress.getText().equals(location)){
weAddress.click();
return true;
}
}
return false;
});
} catch (Exception ex) {
ex.printStackTrace();
}
return false;
}
There is one edge case here. Lets say your expected search result is already part of default search results, the above code may throw StaleElementReferenceException. But we have handled it so I hope it would work fine. Please let me know if you have any issues.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ahamed Abdul Rahman |
