1
I have been using thread.sleep(9000) almost after every line of code in selenium which is making me wait for long.Can anybody suggest me an alternate way to reduce this.As my application is taking time load a page it needs to wait until a particular page is loaded to perform any action.

WebElement un = driver.findElement(By.id("login_username_id"));
            un.sendKeys(username);
            WebElement pwd = driver.findElement(By.id("login_password_id"));
            pwd.sendKeys(password);

            try {
                Thread.sleep(25000);
            } catch (InterruptedException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            driver.findElement(By.id("login_submit_id")).click();
            try {
                Thread.sleep(9000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

I want to reduce the usage of thread.sleep after every line and use one common function so that it waits whenever required.

Vdev
  • 101
  • 1
  • 5
  • 17

3 Answers3

2

Hi please use universal wait in your script i.e implicit wait

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

above line tell selenium to wait for maximum of 10 seconds for each and every webelement before throwing any error (note you can increase or decrease seconds it depends upon you)

explicit wait : when you want to wait for a specific webelement (use this when you think a particular element takes more then usual time to load then only)

WebDriverWait wait = new WebDriverWait(driver, timeout);
wait.until(ExpectedConditions.visibilityOfElementLocated(your element));
Rajnish Kumar
  • 2,828
  • 5
  • 25
  • 39
  • i used this statement but what if i want to wait after every statement?how should i use it?Say my element is so(By.id("login_password_id")); – Vdev May 25 '16 at 10:05
  • in that case use universal wait at the time of browser creation like WebDriver driver = new FirefoxDriver(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); – Rajnish Kumar May 25 '16 at 10:10
  • but driver.manage does not help,,,it expects thread.sleep after every line – Vdev May 25 '16 at 10:19
  • please increase the time of driver.manage to 50 seconds look if that helps – Rajnish Kumar May 25 '16 at 10:25
  • No it does not help,,,i need to write something in common to avoid this.I am getting this error. Exception in thread "main" org.openqa.selenium.WebDriverException: Element is not clickable at point (858, 409.933349609375). Other element would receive the click:
    Command duration or timeout: 147 milliseconds Build info: version: '2.52.0', revision: '4c2593c', time: '2016-02-11 19:06:42' System info: host: 'desktop-svc71', ip: '10.2.2.54', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_73'
    – Vdev May 25 '16 at 10:34
  • WebDriverWait wait = new WebDriverWait(driver, timeout); wait.until(ExpectedConditions.visibilityOfElementLocated(your element)); In this can u tell how i can write for my above code? – Vdev May 25 '16 at 10:35
  • hi as per your error problem is not with implicit wait,its working fine selenium is able to click .but the element on which you want to click is yet not have fixed [position in the html so another element will receive the click – Rajnish Kumar May 25 '16 at 10:44
  • To solve the error Exception in thread "main" org.openqa.selenium.WebDriverException: Element is not clickable at point (858, 409.933349609375). please do one thing plz change your browser form chrome to firefox or follow this link http://stackoverflow.com/questions/37388866/selenium-element-is-not-clickable-at-point/37389577#37389577 – Rajnish Kumar May 25 '16 at 10:47
2

use the below example:

public class Main{

static WebDriver driver = new FirefoxDriver();

public static void main(String [] args) throws InterruptedException{

    WebElement un = driver.findElement(By.id("login_username_id"));
    un.sendKeys(username);
    WebElement pwd = driver.findElement(By.id("login_password_id"));
    pwd.sendKeys(password);

    waitForElement(By.id("ur id"),60);

    driver.findElement(By.id("login_submit_id")).click();

    waitForElement(By.id("ur id"),60);


}

/**
 * wait until expected element is visible
 *
 * @param   expectedElement     element to be expected
 * @param   timeout             Maximum timeout time
 */
public static void waitForElement(By expectedElement, int timeout) {
    try {
        WebDriverWait wait = new WebDriverWait(driver, timeout);
        wait.until(ExpectedConditions.visibilityOfElementLocated(expectedElement));

    } catch (Exception e) {
        e.printStackTrace();
        //System.out.println("print ur message here");
    }
}
}

if u have any confusion, let me know.

noor
  • 2,954
  • 2
  • 16
  • 29
  • where should i mention the element ,i mean which line and can u give me an example ,is it like this? Say if i wan to wait after this statement how should i write? driver.findElement(By.id("login_submit_id")).click(); – Vdev May 25 '16 at 09:55
  • use method like : waitForElement(By.id("login_submit_id") , 60); – noor May 25 '16 at 10:09
  • u can also use By.cssSelector or By.class .... this will wait for 60 seconds maximum...but if the element is found after 1 second, the wait will finish – noor May 25 '16 at 10:10
  • like i have my code this way..can u add ur code and send me public static void main(String[] args) throws InterruptedException { WebDriver driver=new FirefoxDriver();WebElement un = driver.findElement(By.id("login_username_id")); un.sendKeys(username); WebElement pwd = driver.findElement(By.id("login_password_id")); pwd.sendKeys(password);/*try { Thread.sleep(25000); } catch (InterruptedException e1) { // TODO Auto-generated catch block e1.printStackTrace(); }*/ driver.findElement(By.id("login_submit_id")).click(); – Vdev May 25 '16 at 10:17
  • i have given a full class example in my updated code. copy and import necessary packages and run it. – noor May 25 '16 at 10:33
  • And after which statement should i use this method?public static void waitForElement(By expectedElement, int timeout)......like after this?WebDriver driver=new FirefoxDriver(); – Vdev May 25 '16 at 10:39
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/112893/discussion-between-noor-and-vdev). – noor May 25 '16 at 10:40
  • is this ok ... @Vdev? – noor May 26 '16 at 06:27
  • Hello noor, i need ur advice regarding one thing,i have implemented data driven framework and written some scripts,what is that i can do next and how do u judge that my code is good enough as per standards? – Vdev Jun 14 '16 at 11:46
  • @Vdev actually i think thats another question....first of all u have to accept the answer if the answer helps u and than u can raise another question with details for solution. stackoverflow does not like too long chat. – noor Jun 14 '16 at 11:55
0

You can put assertion saying that "Whether particular element displayed or not" so that web driver will spend time to search that element which will create delay in execution.

Ex: A page may contain some button make it as target and tell web driver to find it.