2

I am getting this error "Element is currently not visible" when I try to input into textbox which is visible I've created a script to signup and I am getting this error randomly sometimes on "password" textbox sometime on "re-password" text box I don't know what is the reason? following is code snippet

ExcelRead objExcel = new ExcelRead("D:\\K - Selenium\\SeleniumWorkspace\\EMAILGENERATOR\\src\\testData\\EmailGeneratorData.xls","CreateAccount");

        int iNoofRows = objExcel.rowCount()-1;

        for(int i =1;i<iNoofRows;i++)
        {                           
        //driver.findElement(By.xpath(".//*[@id='idb']")).clear();
        Thread.sleep(5000);
        driver.findElement(By.xpath(".//*[@id='idb']")).sendKeys(objExcel.getCellData("FName", i));


        //driver.findElement(By.xpath(".//*[@id='idd']")).clear();
        Thread.sleep(2000);
        driver.findElement(By.xpath(".//*[@id='idd']")).sendKeys(objExcel.getCellData("LName", i));
        //Gender
        Thread.sleep(2000);
        driver.findElement(By.xpath(".//*[@id='idf']")).sendKeys(objExcel.getCellData("Gender", i));
        //Month
        Thread.sleep(2000);
        driver.findElement(By.xpath(".//*[@id='id14']")).sendKeys(objExcel.getCellData("Month", i));
        //Day
        Thread.sleep(2000);
        driver.findElement(By.xpath(".//*[@id='id11']")).sendKeys(objExcel.getCellData("Day", i));
        //Year
        Thread.sleep(2000);
        driver.findElement(By.xpath(".//*[@id='id15']")).sendKeys(objExcel.getCellData("Year", i));
        //Country
        Thread.sleep(2000);
        driver.findElement(By.xpath(".//*[@id='id16']")).sendKeys(objExcel.getCellData("Country", i));
        //Email
        Thread.sleep(2000);
        driver.findElement(By.xpath(".//*[@id='id1e']")).sendKeys(objExcel.getCellData("Email", i));
        //password
        Thread.sleep(2000);
        driver.findElement(By.id("id28")).sendKeys(objExcel.getCellData("Password", i));
        //re-password
        Thread.sleep(2000);
        driver.findElement(By.xpath(".//*[@id='id2b']")).sendKeys(objExcel.getCellData("Re-Password", i));
        //Security Q
        Thread.sleep(3000);
        //driver.findElement(By.id("id33")).click();
        driver.findElement(By.id("id33")).sendKeys(objExcel.getCellData("SecQ", i));
        //Ans
        Thread.sleep(2000);
        driver.findElement(By.id("id39")).click();
        driver.findElement(By.id("id39")).sendKeys(objExcel.getCellData("Ans", i));

        Thread.sleep(30000);
        //Captcha
        //driver.findElement(By.xpath(".//*[@id='recaptcha-anchor']/div[5]")).click();
        //Button Click
        driver.findElement(By.xpath(".//*[@id='id3e']")).click();
        }
  • using sleep can make the test brittle. can you use a wait until element visible? – David Cain Apr 5 '16 at 23:04
2

I would suggest not to use Thread.sleep() method, instead you can use the implicit wait, add following line of code after driver is declared.

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

and remove all Thread.sleep() methods from the code.

Difference between implicit wait and Thread.sleep() is Thread.sleep() method will wait until given time then it will search for the element which unnecessarily increase the execution time, where as implicit wait will keep on searching for the element until given time then it will throw an exception if element is not found and if element is found it will continue the execution immediately

1

There is several issues here.

An implicit wait would do far better than the Thread.sleep()

Using Xpath to search by id. Just use by.id() it will be far faster

The ids, to me, appear to be dynamically created which will cause them to break. If the rest of the script works I would presume this is not the issue though.

Use new WebDriverWait(driver,10).until(ExpectedConditions.visibilityOfElementLocated(ElementBy)) to wait x (10 in this case) seconds until the element is visible

Use the new Actions(driver).moveToElement(el) API to ensure the element is scrolled into the view

Note:

I instantiated the above as new Objects so the code is directly usable. I would recommend putting them into a shared/helpers class to avoid having to deal with them on a regular basis. With this way, they just work every time and are easy.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.