Selenium WeDriver FluentWait - java

I'm having a problem in my script. I'm using Selenium WebDriver to drive a webpage, but i'm getting ElementNotFound exceptions quite regularly. The page takes a second or two to load.
My code is the following:
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
.withTimeout(10, TimeUnit.SECONDS)
.pollingEvery(1, TimeUnit.SECONDS)
.ignoring(NoSuchElementException.class);
try
{
WebElement username = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[#class='gwt-TextBox']")));
username.sendKeys(usernameParm);
}
catch (Exception e) {
e.printStackTrace();
}
The exception still gets thrown after a second or so. Then if i test it by running the following:
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
.withTimeout(10, TimeUnit.SECONDS)
.pollingEvery(1, TimeUnit.SECONDS)
.ignoring(NoSuchElementException.class);
try
{
WebElement username = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[#class='gwt-TextBox1']")));
username.sendKeys(usernameParm);
}
catch (Exception e) {
e.printStackTrace();
}
Knowing that TexBox1 doesn't exist, then it throws the same exception. It doesn't appear to be waiting. In the second instance i would expect it to time out, and not throw ElementNotFoundException.
My implementation is probably wrong.

Checkout my post on this topic: https://iamalittletester.wordpress.com/2016/05/11/selenium-how-to-wait-for-an-element-to-be-displayed-not-displayed/. There are code snippets there. Basically my suggestion is not to use FluentWait, but instead:
WebDriverWait wait = new WebDriverWait(driver, TIMEOUT);
ExpectedCondition elementIsDisplayed = new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver arg0) {
try {
webElement.isDisplayed();
return true;
}
catch (NoSuchElementException e ) {
return false;
}
catch (StaleElementReferenceException f) {
return false;
}
}
};
wait.until(elementIsDisplayed);
Define TIMEOUT with whatever timeout value seems ok for you (i believe you said 10 seconds in your initial issue).

Yes, i'm answering my own question. Figured out what the problem was.
I had imported java.lang.util.NoSuchElementException
I had told FluentWait to ignore NoSuchElementException
What was actually being thrown was org.openqa.selenium.NoSuchElementException
Once i changed it, it seems to work just fine.
Before i figured this out, i also implemented this:
for (int i = 0; i< 10; i++){
if (B_Username){
break;
} else {
try {
WebElement username = driver.findElement(By.xpath("//*[#class='gwt-TextBox']"));
B_Username = true;
username.sendKeys(usernameParm);
} catch (NoSuchElementException e) {
System.out.println("Caught exception while locating the username textbox");
System.out.println(i);
try {
Thread.sleep(500);
} catch (InterruptedException ee) {
System.out.println("Somthing happened while the thread was sleeping");
}
}
}
}
which worked fine as well.
Maybe this will help someone else another time.
Thanks to everyone who answered.

Related

NoSuchElementException can't be thrown

I'm trying to use an element for testing and I want to continue the test if the element even couldn't be found.
I used NoSuchElementException for this part of my codes.
Here is what I've tried before:
try {
WebElement site_width_full_width = wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.cssSelector("label[for=site_width-full_width]")));
site_width_full_width.click();
Thread.sleep(1000);
System.out.println("FullWidth Label Found!");
} catch (NoSuchElementException e) {
System.out.println("FullWidth Label not found!");
System.out.println(e);
}
But when the element isn't available it can't be thrown into the NoSuchElementException and all test breaks and fails.
What's the solution and how could I continue the test when the element isn't available.
Thanks in advance.
You might be getting the exception of a different derived class type. You can catch it using the parent class 'Exception' and then further drill down the exact exception type.
try using;
try
{
WebElement site_width_full_width = wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.cssSelector("label[for=site_width-full_width]")));
site_width_full_width.click();
Thread.sleep(1000);
System.out.println("FullWidth Label Found!");
}
catch (Exception e)
{
if (e instanceof NoSuchElementException)
{
System.out.println("FullWidth Label not found!");
System.out.println(e);
}
else
{
System.out.println("Unexpected exception!");
System.out.println(e);
}
}
Hope this helps.
You can try with its parent classes like Throwable or Exception in catch block. In my case, I am Throwable in catch block which works as expected
You are catching a NoSuchElementException, but an explicit wait throws a TimeoutException if nothing is found. To get what you have working you should modify your code to do the following:
try {
WebElement site_width_full_width = wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.cssSelector("label[for=site_width-full_width]")
));
site_width_full_width.click();
System.out.println("FullWidth Label Found!");
} catch (NoSuchElementException | TimeoutException e) {
System.out.println("FullWidth Label not found!");
System.out.println(e);
}
However using Try/Catch for execution flow is generally a code anti-pattern. You would be much better off doing something like this:
List<WebElement> site_width_full_width =
driver.findElements(By.cssSelector("label[for=site_width-full_width]"));
if (site_width_full_width.size() > 0) {
System.out.println("FullWidth Label Found!");
site_width_full_width.get(0).click();
} else {
System.out.println("FullWidth Label not found!");
}
Webdriver already provide the way to deal this problem in much simpler way. you can use following way
WebDriverWait wait= new WebDriverWait(driver, TimeSpan.FromSeconds(120));
wait.IgnoreExceptionTypes(typeof(NoSuchElementException), typeof(ElementNotVisibleException));
WebElement site_width_full_width = wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.cssSelector("label[for=site_width-full_width]")));
site_width_full_width.click();
Thread.sleep(1000);
System.out.println("FullWidth Label Found!");
Note : You can add all types of exception need to be ignored.

Interrupt loading of site by condition in Selenium WebDriver

There are multiple files which should be uploaded and waiting for output. It opens up an AJAX like window during processing. If processing takes too much time, Close button should be clicked on this window and file should be submitted again.
I try to use code below, but doesn't click Close button in 10 seconds.
public void clickOnSendButton() throws InterruptedException {
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement webElement;
try {
driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
driver.findElement(sendButton).click();
log.info("Processing in progress!");
webElement = wait.until(ExpectedConditions.presenceOfElementLocated(By.className("button-download")));
} catch (TimeoutException ex) {
webElement = null;
} finally {
driver.manage().timeouts().implicitlyWait(90, TimeUnit.SECONDS);
}
if (webElement == null) {
driver.findElement(popUpClose).click();
TimeUnit.SECONDS.sleep(1);
driver.findElement(sendButton).click();
}
}
Try using 'visibilityOfElementLocated' condition instead of 'presenceOfElementLocated' as below:
webElement = wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("button-download")));
Using the loop:
try {
driver.findElement(By.id("button-submit")).click();
Thread.sleep(3000);//3 seconds
log.info("Processing in progress!");
for(int i=0; i<10;i++){
try{
webElement = driver.findElement(By.className("button-download"));
} catch (Exception e){e.printStackTrace();}
if(webElement.isDisplayed())
break;
else
Thread.sleep(1000);
}
} catch (TimeoutException ex) {ex.printStackTrace();} finally {
driver.manage().timeouts().implicitlyWait(90, TimeUnit.SECONDS);
}
if (!webElement.isDisplayed() ) {
driver.findElement(By.xpath("/html/body/div[4]/div[1]/button/span[1]")).click();
Thread.sleep(2000);
driver.findElement(By.id("button-submit")).click();
}

Click webelement until hidden

I have a web application where I press submit button until data available on the table.When in my no data are available then submit button hidden.So we can get logic until submitting button hides we will click.And when button, not available we show on success message and load next browser Url.
for (k=0;k>30;k++) {
try {
driver.findElement(By.xpath(".//*[#id='content']/input")).click();
driver.switchTo().alert();
driver.switchTo().alert().accept();
Thread.sleep(20000);
} catch (org.openqa.selenium.NoSuchElementException e){
System.out.println(""+location+" Done");
}
}
Here driver.findElement(By.xpath(".//*[#id='content']/input")).click(); this line click my submit button.And after submit one browser alert shows thats why i accept this. In this loop for (k=0;k>30;k++) Blindly i take 30..Is there any logic or any sugggestion how can i manage this...
You can use existence of the element in a way something like this:-
By by = By.xpath(".//*[#id='content']/input");
List<WebElement> buttons = driver.findElement(by);
while(buttons !=null && !buttons.isEmpty()) {
WebElement yourButton = buttons.get(0); // assuming only and the first element in the list
yourButton.click();
driver.switchTo().alert();
driver.switchTo().alert().accept(); // ideally this should be sufficient
Thread.sleep(20000);
buttons = driver.findElement(by);
}
Below code may help you.
try {
while(driver.findElement(By.xpath(".//*[#id='content']/input")).isDisplayed()){
driver.findElement(By.xpath(".//*[#id='content']/input")).click();
driver.switchTo().alert();
driver.switchTo().alert().accept();
Thread.sleep(20000);
}
}
catch (org.openqa.selenium.NoSuchElementException e){
System.out.println(""+location+" Done");
}
another solution with findElements,
while(driver.findElements(By.xpath(".//*[#id='content']/input")).size()>0)
{
try {
driver.findElement(By.xpath(".//*[#id='content']/input")).click();
driver.switchTo().alert();
driver.switchTo().alert().accept();
Thread.sleep(20000);
}
catch (org.openqa.selenium.NoSuchElementException e){
System.out.println(""+location+" Done");
}
}
Implement below Logic :
public void test() throws InterruptedException
{
WebElement button = driver.findElement(By.xpath(".//*[#id='content']/input"));
while(checkButton(button))
{
button.click();
driver.switchTo().alert().accept();
Thread.sleep(1000);
}
}
public static boolean checkButton(WebElement element)
{
if(element.isDisplayed())
return true;
else
return false;
}
It will click until your element get invisible once it your can perform your action further. Hope this will help.
Simply checked with isDisplayed() to check whether element is displayed or not
WebElement ele=driver.findElement(By.xpath(".//*[#id='content']/input"));
//check element is present or not
try {
if(ele.size()>0){
if(ele.isDisplayed()){
ele.click();
}
}
//switch to alert and perform operation
driver.switchTo().alert();
driver.switchTo().alert().accept();
Thread.sleep(20000);
}
catch (Exception e){
System.out.println(""+location+" Done");
}
Following way I solve my Problem..Hope Its will help to next visitor
do
{
try {
driver.findElement(By.xpath(".//*[#name='yt1']")).click();
driver.switchTo().alert();
driver.switchTo().alert().accept();
Thread.sleep(20000);
driver.switchTo().alert();
driver.switchTo().alert().accept();
Thread.sleep(2000);
driver.navigate().refresh();
}
catch (org.openqa.selenium.NoSuchElementException e){
System.out.println(""+location+" Done");
}
}
while ((driver.findElements(By.xpath(".//*[#class='google_data_save']")).size()>0));

Best practice - Generic webdriverWait till loading popup exist, if error appears fail the test

My problem is I have customWaitMethods such as:
public void waitForLoading(WebElement loadingElement, WebElement errorElement) {
long timeOut = Long.parseLong(PropertyReader.getInstance().getProperty("DEFAULT_TIME_OUT"));
try {
WebDriverWait wait = new WebDriverWait(DriverFactory.getInstance().getDriver(), timeOut);
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.id(loadingElement.toString())));
if (errorElement.isDisplayed()) {
throw new TestException();
}
} catch (TimeoutException e) {
System.out.println("Timed out after default time out");
} catch (TestException e) {
System.out.println("Unexpected error occurred, environment error");
e.printStackTrace();
}
}
I need some generic customWait methods. I do a search, but several cases need to be handled. Error msg appear -> fail the test. wait for the loading content, and it disappeared, -> check the search result.
How can I extend this code if I would like to check continuously some error_message element appears as well and in this case I would throw an exception? So independently I can handle the timeout exception and the other, error msg?
This sript is failing because of the IF. ErrorElement does not appear on the page, ---> nosuchelementException
You can catch different Exceptions as you see fit. In your case, you want to catch the TimeoutException to handle time outs. Then catch a different type of exception to handle the error message:
public void waitForLoading() {
long timeOut = Long.parseLong(...);
try {
WebDriverWait wait = new WebDriverWait(...);
wait.until(ExpectedConditions.invisibilityOfElementLocated(...));
if (<error-message-appears>) {
throw new CustomErrorMessageAppearedException();
}
} catch (TimeoutException e) {
System.out.println("Timed out after...");
} catch (CustomErrorMessageAppearedException e) {
// handle error message
}
}
The easiest approach I see is:
public void waitForLoading() {
long timeOut = Long.parseLong(PropertyReader.getInstance().getProperty("DEFAULT_TIME_OUT"));
try {
WebDriverWait wait = new WebDriverWait(DriverFactory.getInstance().getDriver(), timeOut);
if (!wait.until(ExpectedConditions.invisibilityOfElementLocated(By.id("wait_element")));)
{
throw new NoSuchElementException();
}
} catch (TimeOutException e) {
System.out.println("Timed out after " + timeOut + "seconds waiting for loading the results.");
}
}

Ping a server without freezing the Thread

I tried to use multiple threads, sadly no luck:
public synchronized boolean pingServer(final String ip, final short port) {
final boolean[] returnbol = new boolean[1];
Thread tt = new Thread(new Runnable() {
#Override
public void run() {
try {
Socket s = new Socket(ip, port);
s.close();
returnbol[0] = true;
} catch (IOException e) {
returnbol[0] = false;
}
}
});
tt.start();
try {
tt.join();
} catch (InterruptedException e) {
tt.stop();
}
tt.stop();
return returnbol[0];
}
The main thread still Freezes for some reason.
Is there a "lagless" way to ping a server?
What exactly did you want to got in
try {
tt.join();
} catch (InterruptedException e) {
tt.stop();
}
block?
Here you joined to parallel thread and waits till this thread will ends (got ping result).
You have next options:
Wait till ping ends
Don't wait... and don't got result
Use some concurrency classes like Future<> to got result (but you will block thread at moment you ask result if it not retrieved yet)
Or you can use some 'callback' function/interface to threw result from inner 'ping' thread.
You will need to remove the following lines from your code.
The tt.join() will force the main thread to wait for tt to finish.
try {
tt.join();
} catch (InterruptedException e) {
tt.stop();
}
tt.stop();
Use a Future instead to get the result for later use

Categories