如何快速制作Selenium chrome web驱动程序

时间:2015-11-03 20:44:19

标签: selenium selenium-webdriver selenium-chromedriver

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

嗨,朋友们,

我有上面的代码,它工作正常,并获得我需要的数据。我唯一的问题是:它很慢。 如果你看一下它所放置的代码非常慢,那就是我在哪里出现错误: catch(NoSuchElementException ex)AND catch(ElementNotVisibleException envex)

被抓住需要多长时间的错误。有人可以帮忙。

1 个答案:

答案 0 :(得分:4)

这是因为.implicitlyWait()设置为200秒。找到元素但未找到时,它将等待200秒。我的建议是删除隐式等待(和Thread.sleep())并使用WebDriverWaitExpectedConditions替换显式等待。

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关于明确和隐含的等待以及为什么你不应该混合它们。