Exemplo n.º 1
0
  public void fetchElementsFromPage(String page, int level) {

    String[] levelTags = getLevelTags(level);

    if (level == 3) {
      try {

        String[] textArray =
            _driver.findElementByCssSelector("div.main.panel").getText().split("[ \t\\x0B\f\r]+");
        int iLen = textArray.length;
        int iMod = iLen / 100;
        for (int i = 0; i <= iMod; i++) {
          String text = "";
          if (i == iMod) text = join(textArray, (i * 100), iLen - 1);
          else text = join(textArray, (i * 100), (i * 100) + 99);

          _elastic.createIndex(page, "Panel", level, text);
        }

      } catch (ElementNotFoundException elemNotFound) {
        elemNotFound.printStackTrace();
      } catch (StaleElementReferenceException staleException) {
        staleException.printStackTrace();
      } catch (Exception e) {
        e.printStackTrace();
      }

      return;
    }

    for (String tagName : levelTags) {
      List<WebElement> elements = _driver.findElementsByTagName(tagName);

      for (WebElement element : elements) {
        try {
          _elastic.createIndex(page, tagName, level, element.getText().trim());
        } catch (StaleElementReferenceException staleException) {
          staleException.printStackTrace();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
  }
Exemplo n.º 2
0
  /**
   * Find web-element for given locator.
   *
   * @param elementName
   * @return
   */
  public WebElement findElement(String elementName) {

    String locator;

    locator = elementName;

    int count = 0;
    while (count < 4) {
      try {
        if (locator.startsWith("link=") || locator.startsWith("LINK=")) {
          locator = locator.substring(5); // remove "link=" from
          // locator
          try {
            if (locator.contains(" ")) return driver.findElement(By.partialLinkText(locator));

            return driver.findElement(By.linkText(locator));
          } catch (Exception e) {
            return null;
          }
        }

        if (locator.startsWith("id=")) {
          locator = locator.substring(3); // remove "id=" from locator
          try {
            return driver.findElement(By.id(locator));
          } catch (Exception e) {
            return null;
          }
        } else if (locator.startsWith("//")) {
          try {
            return driver.findElement(By.xpath(locator));
          } catch (Exception e) {
            return null;
          }
        } else if (locator.startsWith("css=")) {

          locator = locator.substring(4); // remove "css=" from
          // locator
          try {
            return driver.findElement(By.cssSelector(locator));
          } catch (Exception e) {
            return null;
          }
        } else if (locator.startsWith("name=")) {

          locator = locator.substring(5); // remove "name=" from
          // locator
          try {
            return driver.findElement(By.name(locator));
          } catch (Exception e) {
            return null;
          }
        } else {
          try {
            return driver.findElement(By.id(locator));
          } catch (Exception e) {
            return null;
          }
        }
      } catch (StaleElementReferenceException e) {
        e.toString();

        count = count + 1;
        // System.out.println("Trying["+
        // count+"] to recover from a stale element :" +
        // e.getMessage());
      }
      count = count + 4;
    }
    return null;
  }