如何使 Selenium chrome 网络驱动程序更快

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();
    }
}

朋友们好,

我有上面的代码,它工作正常并获取我需要的数据。我唯一的问题是:它很慢。 如果您查看代码,他放置的速度非常慢的地方就是我发现错误的地方: 赶上(NoSuchElementException ex)和 赶上(ElementNotVisibleException envex)

需要很长时间才能得到错误caught.Can请有人帮忙。

因为.implicitlyWait()设置为200s。当一个元素被定位但没有找到时,它会等待200s。我的建议是删除隐式等待(和 Thread.sleep()s)并使用 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 关于显式和隐式等待以及为什么不应该混合使用它们。