Fluent wait not ignoring exceptions - selenium

I am using a fluent wait so I can ignore certain exceptions for a certain time, especially the ElementClickInterceptedException
This is how I declare the wait:
private Wait<WebDriver> initiateWebDriverWait(int timeoutSeconds) {
List allExceptions = new ArrayList();
allExceptions.add(NoSuchElementException.class);
allExceptions.add(ElementNotVisibleException.class);
allExceptions.add(StaleElementReferenceException.class);
allExceptions.add(ElementClickInterceptedException.class);
return new FluentWait<>(driver)
.withTimeout(Duration.ofSeconds(timeoutSeconds))
.pollingEvery(Duration.ofMillis(100))
.ignoreAll(allExceptions);
}
usage:
public void waitForElementThenClick(WebElement webElement, int timeOutSeconds) {
waitForElementToBeClickable(webElement, timeOutSeconds);
webElement.click();
}
public void waitForElementToBeClickable(WebElement webElement, int timeoutSeconds) {
Wait<WebDriver> wait = initiateWebDriverWait(timeoutSeconds);
wait.until(ExpectedConditions.elementToBeClickable(webElement));
}
So when I'm using waitForElementThenClick I still get
org.openqa.selenium.ElementClickInterceptedException: element click intercepted: Element ... is not clickable at point (1338, 202). Other element would receive the click:
This is some random overlay that is there for just a smallest fraction of time, and I could add a 100ms wait and whatnot, but my main issue is why am I even seeing this exception, when I specifically said it to ignore it for at least 5 seconds? And it's not waiting these 5 seconds, so this is not a timeout thing.
Any ideas?
is the webElement.click(); throwing the exception? if So, why the waitForElementToBeClickable returned true?
Thanks

If webdriver says the element is clickable, it's not always mean that element is really clickable. It's can be while the target element is covered by another element.
Here is an example. Open https://stackoverflow.com/jobs?so_medium=StackOverflow&so_source=SiteNav . Let's try to check if this element is clickable and click it:
But, we hide this element under dropdown like that:
There is xpath: By.xpath("//*[text()='Developer jobs']")
So, if we check this element for condition ExpectedConditions.elementToBeClickable, it will return true(like element is clickable). But while you execute element.clickm you will occur an ElementClickInterceptedException.

The exceptions list is considered only while until is waiting for the condition to be true. The click() occurs after that, so there is nothing to catch the ElementClickInterceptedException.
elementToBeClickable checks if the element is visible, which will be true if even part of the element is visible, and if the element is enabled, that will be true unless it has explicit disabled attribute, but Selenium tries to click in the middle of the element which might be covered. To pass this problem you can wait for the overlay to disappear and then wait for the element to be clickable
wait.until(ExpectedConditions.invisibilityOfElementLocated(ovferlayLocator));
waitForElementThenClick();

Related

waiting for an element while condition is still fullfilled

The wait helpers are very useful functions. But it seems they can wait only for an element to exist (Until...)
Is there a wait to say "wait while condition is still fullfilled" ?
Example, click some element and wait for some other element to disappear
You can try (at least with Java, I'm not sure if it's in the other languages) is the ExpectedConditions.not the method, which you can wrap around another ExpectedConditions.
An example would be something like:
new WebDriverWait(driver, 20).until(ExpectedConditions.not(ExpectedConditions.presenceOfElementLocated(By.cssSelector('#loading-spinner'))));
or you can try
new WebDriverWait(driver, 20).until(ExpectedConditions.invisibilityOfElementLocated(By.cssSelector('#loading-content')))
Selenium in Java has
wait.until(ExpectedConditions.invisibilityOfElementLocated(element));
and
wait.until(ExpectedConditions.stalenessOf(element));
methods.
Selenium in Python has invisibility_of_element_located and staleness_of methods too.
So you can create a method clicking on some element and waiting for it to disappear, like this:
public void clickVisibleDisappear(By element){
wait.until(ExpectedConditions.visibilityOfElementLocated(element)).click();
wait.until(ExpectedConditions.invisibilityOfElementLocated(element));
}
Or clicking on one element and waiting for some other element to disappear as you asked, like this:
public void clickVisibleDisappear(By element1, By element2){
wait.until(ExpectedConditions.visibilityOfElementLocated(element1)).click();
wait.until(ExpectedConditions.invisibilityOfElementLocated(element2));
}

selenium element.click() not working (doesn't click)

String selector = ".rmcAlertDialog .buttons :first-child";
RemoteWebElement selection = (RemoteWebElement) driver.findElement(By.cssSelector(selector));
WebDriverWait wait = new WebDriverWait(driver, 60);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(selection));
if (element == selection) selection.click();
But the element in question (a button) is not responding to the click.
If I click the button manually it works so its not the web page at fault, but the automation.
I have verified the button is there by comparing it's text content.
updated for clarification
This code works (or worked) for most buttons. The code is from a script interpreter which is parsing:-
select ".rmcAlertDialog .buttons :first-child" click
This code was working prior to more recent versions of chrome/selenium/chromedriver.
The code now doesn't work for some buttons.
selection.click() IS being called (verified in a debugger), as element will always equal selection, it just is not working.
.buttons is the class name of the container div for the button(s)
The selector is not directing to the element with button class. You have a space between .button and :first-child in the selector. Remove the space. The given selector is searching for a child element of the tag with button class. But I'm assuming you are trying to click on the first element with button class not the child node of the button class element.
Use this:
String selector = ".rmcAlertDialog .buttons:first-child";
I think the main reason it's failing is because your if statement will never be true. I've never done any comparisons like this but you can simplify your code significantly and still get the desired effect.
A few suggestions:
Don't define locators as Strings, define them as Bys. The By class is defined for just such a task and makes using and passing them around MUCH easier.
String selector = ".rmcAlertDialog .buttons:first-child";
would turn into
By locator = By.cssSelector(".rmcAlertDialog .buttons:first-child");
Note the correction that S Ahmed pointed out in his answer.
You don't need to find the element to wait for it to be clickable. There is an overload that takes a By locator, use that instead.
RemoteWebElement selection = (RemoteWebElement) driver.findElement(By.cssSelector(selector));
WebDriverWait wait = new WebDriverWait(driver, 60);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(selection));
becomes
WebElement element = new WebDriverWait(driver, 60).until(ExpectedConditions.elementToBeClickable(locator));
Skip the RemoteWebElement and WebElement comparison. I don't think this will work and there's no need for it anyway. Your locator will locate the same element consistently.
So your final code should look something like
By locator = By.cssSelector(".rmcAlertDialog .buttons:first-child");
new WebDriverWait(driver, 60).until(ExpectedConditions.elementToBeClickable(locator)).click();

Wait for element for certain amount of time

I have been experiencing one issue during test automation implementation. Particularly test checks if a hamburger menu is displayed.
So far I defined the element and subelement, and I need to really wait just one second, and not to waste time If I know that element will not be displayed after several seconds.
WebDriverWait wait = new WebDriverWait(getDriver(), 1);
WebElement hamMenu = el.findElement(By.xpath(HAMBURGER_MENU_GENERAL_XPATH));
How to implement the method findElement in the way it will try to find the element in one sec? I do not wish to stay longer... Thanks
Try this -
WebElement elem = new WebDriverWait(driver, 1).until(ExpectedConditions.visibilityOfElementLocated(By.xpath(HAMBURGER_MENU_GENERAL_XPATH)));
If I summarize your requirement is as follows :
Check if a hamburger menu is displayed only for 1 second : You need WebDriverWait with proper ExpectedConditions
Element may/not not be displayed after several seconds : You need to wrapup your code in a try-catch {} block to be able to proceed further in absence of the hamburger.
Youe effective code can be :
try {
WebElement hamburger = new WebDriverWait(getDriver(), 1).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("HAMBURGER_MENU_GENERAL_XPATH")));
System.out.println("Hamburger is displayed in 1 sec");
//rest of your code
} catch (NoSuchElementException e){
System.out.println("Hamburger wasn't displayed in 1 sec");
//rest of your code
}
I got it. Thanks for all hints. This will wait just only for short time which is intended.
public boolean isHamMenuDisplayed(){
getDriver().manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
if (el.findElements(By.xpath(HAMBURGER_MENU_GENERAL_XPATH)).size() == 0)
return false;
else
return true;}

wait until function not working

I am clicking one element and I am using wait function to identify the next element. This is the wait function.
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("some xpath")));
After identifying the element, I carry out other actions but when I click for the 1st element it will lead to next page so by the time page loads the wait function is applying for the current page and giving exception. Is there any solution for this? I tried
Browser.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
before wait but it's not working. It will only work if I use thread.sleep(1000) before wait but I dont want use thread.sleep().
I'm guessing the problem is with the fact that you are using
ExpectedConditions.presenceOfElementLocated(...)
Presence just means that the element is present in the DOM, not that it's visible, enabled, clickable, etc. I would suggest that you change to wait to match what you want to do with the element you are looking for. If you want to click it, wait for it to be clickable. If you want to get text from it, wait for it to be visible, etc.
Another issue may be that you are intending to wait for an element on page 2 but an element matches that locator on page 1. One solution is to find a unique element on page 2, wait for it to be visible, and then wait for your desired element. That way you ensure that you are on the correct page before waiting for the desired element on page 2.
I think there are 2 waits in this situation:
Wait for next page loaded
Wait for specified element loaded in the new page
Below is an option to wait for page loaded:
public void waitForLoad() {
ExpectedCondition<Boolean> condition = webDriver -> webDriver.getCurrentUrl().contains(getPageUrl());
WebDriverWait wait = new WebDriverWait(driver, pageLoadTimeout);
wait.until(condition);
}
Then wait for next element visible:
protected void waitFor(By by) {
ExpectedCondition<Boolean> condition = webDriver -> !webDriver.findElements(by).isEmpty();
WebDriverWait wait = new WebDriverWait(driver, pageLoadTimeout);
wait.until(condition);
}
or using other solutions:
public WebElement elementToBeClickable(By locator, int timeout) {
try {
return getWebDriverFluentWait(timeout)
.until(ExpectedConditions.elementToBeClickable(locator));
} catch (Exception e) {
return null;
}
}
with:
private Wait<WebDriver> getWebDriverFluentWait(int timeout) {
return new FluentWait<WebDriver>(driver)
.withTimeout(timeout, TimeUnit.SECONDS)
.pollingEvery(1, TimeUnit.SECONDS)
.ignoring(NoSuchElementException.class);
}
I think you want to click on element, so use elementToBeClickable
WebDriverWait wait=new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(locator))
I also Have the same Problem
But using this I solved
WebElement element = (new WebDriverWait(driver, 30)).until(ExpectedConditions.elementToBeClickable(By.xpath(".//*[#class='IM_overlay']"))); //it will wait 30 second for get this element
element .click();
//You can Do any operator here
If the element that is to be clicked is in the next(new) page, you'll have to use the windows iterator.
You could use this code:
Set <Strings> ids = driver.getWindowHandles();
Iterator <String> it = ids.iterator();
String currentPage = it.next();
String newPage = it.next();
driver.switchTo().window(newPage);//this will switch to the new window. Use your 'wait' condition now and do all the operations
//now to switch back to the previous(current) window, you could use the below code
driver.switchTo().window(currentPage);

Waiting for elements with Selenium WebDriver

I am automating a web page that essentially has a button which, each time it is pressed, creates a new text field.
I am using the FindBy annotation with PageFactory to find the list of text fields.
I have a synchronization problem when I press the button several times, thus creating several text fields, and then try to write into one of them. Stepping through the debugger it works fine, but out of the debugger there is a delay before the FindBy finds all the text fields.
My current workaround performs sleeps until the required number of text fields are found but I find this quite unsatisfactory. Any suggestions how the synchronization could best be done?
#FindBy(how= How.XPATH, using="//*[contains(#id, 'TextField')]")
private List<WebElement> textFields;
:
:
public void enterText(Integer index, String text){
int attempts = 0;
// Check every 10th of a second for 10 seconds if all the textFields have been found
while ((textFields.size() <= index) && attempts < 100){
Thread.sleep(100);
}
textFields.get(index).sendKeys("blah blah");
}
First of all use Implicit wait for all element present in script.
This wait is wait for all element present in the script
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
Initiate above just after get.("URL");
Now for specific element which need additional time you can use below code:-
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("someid")));
Refer below:-
http://toolsqa.com/selenium-webdriver/wait-commands/
Hope it will help you :)