Pankaj Srivastava
Pankaj Srivastava

Reputation: 113

How to make Selenium chrome web driver fast

@Before
public void setUpRestClient() throws InterruptedException {

    try {

        driver.manage().timeouts().implicitlyWait(200, TimeUnit.SECONDS);
        List<String> indexSuburbStatePost = new ArrayList<String>();

        indexSuburbStatePost.add("ABC");
        indexSuburbStatePost.add("YYZ");

        for (int j = 0; j < indexSuburbStatePost.size(); j++) {
            System.out.println("for loop===>"   + indexSuburbStatePost.get(j));


            mySchoolDriver(indexSuburbStatePost.get(j));

            Thread.sleep(10000);
            List<String> indexNumberSchool = new ArrayList<String>();
            List<String> indexNameSchool = new ArrayList<String>();

            List<WebElement> elementsAdv = driver.findElements(By.xpath("//table[@id='SearchResults']/tbody/tr/td"));
            System.out.println("Test advance elements number of elements: " + elementsAdv.size());

            writeToFile(indexSuburbStatePost.get(j));

            for (WebElement eleadv : elementsAdv) {

                System.out.println("Text adv======>" + eleadv.getText());
                if (eleadv.getText().equalsIgnoreCase("Primary")
                        || eleadv.getText().equalsIgnoreCase("Secondary")
                        || eleadv.getText().equalsIgnoreCase("Government")
                        || eleadv.getText().equalsIgnoreCase("Combined")
                        || eleadv.getText().equalsIgnoreCase("Special")
                        || eleadv.getText().equalsIgnoreCase(
                                "Non-government")) {

                } else {
                    indexNameSchool.add(eleadv.getText());
                }

            }

            Iterator<String> indexNumberSchoolIteratorAA = indexNameSchool
                    .iterator();

            for (int k = 0; k < indexNameSchool.size(); k++) {
                System.out.println("indexNumberSchoolIterator AA===>"+ indexNumberSchoolIteratorAA.next());
                writeToFile(indexNameSchool.get(k));

            }
            List<WebElement> elementscss = driver.findElements(By.cssSelector("#SearchResults tr a"));
            for (WebElement e : elementscss) {
                String url = e.getAttribute("href");
                System.out.println(url.substring(url.length() - 5));
                indexNumberSchool.add(url.substring(url.length() - 5));
            }

            for (int i = 0; i < indexNumberSchool.size(); i++) {
                Thread.sleep(10000);
                writeToFile(indexNumberSchool.get(i));

                try {
                    driver.findElement(By.xpath("//a[@href=\"/Home/Index/"+ indexNumberSchool.get(i) + "\"]")).click();
                    driver.findElement(By.id("IAccept")).click();
                    driver.findElement(By.className("captch-submit")).click();
                } catch (NoSuchElementException ex) {
                     do nothing, link is not present, assert is passed 
                    System.out.println(" NoSuchElementException======>" + ex.getMessage());

                    mySchoolDriver(indexSuburbStatePost.get(j));

                    Thread.sleep(10000);
                    driver.findElement(By.xpath("//a[@href=\"/Home/Index/"+ indexNumberSchool.get(i) + "\"]")).click();
                }
                driver.findElement(By.id("NaplanMenuButton")).click();
                try {
                    driver.findElement(By.xpath("//*[@id=\"NaplanMenu\"]/ul/li[2]/a")).click();
                } catch (ElementNotVisibleException envex) {
                    System.out.println(" ElementNotVisibleException======>"+ envex.getMessage());
                }

                List<WebElement> elements = driver.findElements(By.xpath("//div[@id='ResultsInNumbersContainer']/table/tbody/tr"));
                System.out.println("Test7 number of elements: " + elements.size());
                BufferedWriter writer = null;
                File f = null;
                for (WebElement ele : elements) {
                    if (ele.getAttribute("class").equalsIgnoreCase( "selected-school-row")) {
                            writeToFile(ele.getText());
                    }
                }
            }

        }

    } catch (Exception e) {
        e.printStackTrace();
        driver.close();
    }

}

private void mySchoolDriver(String indxSuburbPost) {
    driver.get("http://www.abc.xyz.com");
    driver.findElement(By.id("SuburbTownPostcodeSearch")).sendKeys(indxSuburbPost);
    driver.findElement(By.id("SuburbTownPostcodeSearchSubmit")).submit();
}

private void writeToFile(String indxSuburbPost) {
    try{


    String filename = "C:/logs/data.txt";
    FileWriter fw = new FileWriter(filename, true); 
    fw.write(indxSuburbPost + "\n");
    fw.write("\n");
    fw.close();
    }catch (Exception e) {
        e.printStackTrace();
    }
}

Hi Friends,

I have above code and it is working fine and getting the data which I need. My only problem is : it is very slow. If you look at the code he place where it is very slow is where I am cathing errors: catch (NoSuchElementException ex) AND catch (ElementNotVisibleException envex)

it takes ages to error to get caught.Can some one please help.

Upvotes: 1

Views: 2076

Answers (1)

JeffC
JeffC

Reputation: 25542

It's because of the .implicitlyWait() set at 200s. When an element is being located and not found, it will wait for 200s. My suggestion is to remove the implicit wait (and Thread.sleep()s) and replace them with explicit waits using WebDriverWait and ExpectedConditions.

WebDriver driver = new FirefoxDriver();
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(...)).click();

// as long as you are OK with the time setting in the above WebDriverWait declaration
// (10 seconds), you can reuse the wait again and again with the same 10s wait.
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(...));
wait.until(ExpectedConditions.elementToBeClickable(...)).click();

Read more about explicit and implicit waits and why you shouldn't mix them.

Upvotes: 5

Related Questions