1

I am currently learning Selenium, and I learned a lot. One thing the community said; is that you need avoiding thread.sleep as much as possible. Selenium uses implicit and explicit waits in replace. Yes, I understand that concept.

Recently I cam across a problem. This is that without a certain action; going from the login page to another page, without the use of a Thread.sleep(1000). Selenium seems too crash: that it can't find a certain element. I find this behaviour strange. So I was thinking that this conflict occurs, because of the login page that firstly wants to redirects to the main page of the website and without the Thread.sleep(1000); it wants to go to the second page but the login page refuses it because it want's to go first to the main page. With that being said is that why Selenium crashes or do you guys see and strange use of code in the example below?

// Currently on a webpage   
     WebElement ui_login_button = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("account-login-button")));
            ui_login_button.click();
//After the click it logs in and redirects to a webpage

Thread.sleep(1000); // why sleep here? (without this Selenium crashes)

   // Go to second page and perform actions

waitForLoad(driver);
driver.navigate().to(URL + "/mymp/verkopen/index.html");

/* -------------------------------------------------------------------

public void waitForLoad(WebDriver driver) {
        ExpectedCondition<Boolean> pageLoadCondition = new
                ExpectedCondition<Boolean>() {
            public Boolean apply(WebDriver driver) {
                return ((JavascriptExecutor)driver).executeScript("return document.readyState").equals("complete");
            }
        };

        //WebDriverWait wait = new WebDriverWait(driver, 30);
        wait.until(pageLoadCondition);
    }

Sorry for the explanation, I tried my best to be clear. English is not my native language. Thanks for your help.

Kind regargds.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Bedirhan
  • 89
  • 1
  • 5
  • What do you exactly mean when you say `Selenium crashes` ? Do you see any exception raised? Can you paste the error stack trace? – undetected Selenium Jul 13 '17 at 18:34
  • DebanjanDB like I said. It raises an exception that it can't find the element on the webpage. And I also explained what my thoughts are on this. I fixed it by putting a sleep in between. Then it does find it, but it is not an elegant solution to fix this. – Bedirhan Jul 13 '17 at 20:08

2 Answers2

1

As per your question and the updated comments It raises an exception that it can't find the element on the webpage, it is very much possible. Additionally when you mention putting a sleep in between is not an elegant solution to fix, that's pretty correct as inducing Thread.sleep(1000); degrades the overall Test Execution Performance.

Now, what I observed in your commented code block to compare document.readyState to complete was a wiser step. But sometime it may happen that, though Web Browser will send document.readyState as complete to Selenium, due to presence of JavaScript and AJAX Calls the elements with whom we want to interact may not be Visible, Clickable or Interactable which in-turn may raise associated Exception.

So, the solution would be inducing ExplicitWait i.e. WebDriverWait. We will induce ExplicitWait for the element with which we want to interact, with proper ExpectedConditions set. You can find documentation about ExplicitWait here.

An Example:

If you want to wait for a button to be clickable the expected code block may be in the following format along with the imports:

import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;        

// Go to second page and wait for the element    
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.id("id_of_the_element")));        
//perform actions
driver.navigate().to(URL + "/mymp/verkopen/index.html");
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

I am guessing that the exception is raised after you've navigated to URL + "/mymp/verkopen/index.html" and started to take some action.

I am speculating that the main issue here is that your waitForLoad() method is not waiting for some Javascript, or other background task, to complete on the page that Login goes to first. So when you navigate to the next page, something is not yet completed, leaving your user authentication in a bad state. Perhaps you need to wait for some AJAX to complete after login before proceeding with your further navigation? Or it would be better to click on a link on that page to trigger the navigation to your target page (as a real user would), rather than directly entering the URL? You might find it helpful to discuss the actual behavior of the web application with developers.

As DebanjanB has pointed out, once you are on your target page you can then use WebDriverWait for the elements on the page where you are taking actions.

Breaks Software
  • 1,721
  • 1
  • 10
  • 15