org.openqa.selenium.NoSuchElementException类的使用及代码示例

x33g5p2x  于2022-01-24 转载在 其他  
字(9.1k)|赞(0)|评价(0)|浏览(185)

本文整理了Java中org.openqa.selenium.NoSuchElementException类的一些代码示例,展示了NoSuchElementException类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。NoSuchElementException类的具体详情如下:
包路径:org.openqa.selenium.NoSuchElementException
类名称:NoSuchElementException

NoSuchElementException介绍

[英]Thrown by WebDriver#findElement(By)and WebElement#findElement(By by).
[中]由WebDriver#findElement(by)和WebElement#findElement(by)抛出。

代码示例

代码示例来源:origin: galenframework/galen

public WebElement findElement(SearchContext searchContext) {
  List<WebElement> elements = searchContext.findElements(by);
  if (next != null) {
    if (index > 0 ) {
      if (index <= elements.size()) {
        return next.findElement(elements.get(index - 1));
      }
    } else {
      if (elements.size() > 0) {
        return next.findElement(elements.get(0));
      }
    }
  } else {
    if (index > 0) {
      if (index <= elements.size()) {
        return elements.get(index - 1);
      }
    } else {
      if (elements.size() > 0) {
        return elements.get(0);
      }
    }
  }
  throw new NoSuchElementException(by.toString() + " | index " + index);
}

代码示例来源:origin: Cognifide/bobcat

/**
 * Selects given options of select component.
 *
 * @param value String value of comma delimited field names which will be selected.
 */
@Override
public void setValue(Object value) {
 selectField.click();
 List<WebElement> options = selectField.findElements(By.cssSelector(SELECT_OPTIONS_CSS));
 options.stream().filter(o -> value.toString().equals(o.getText()))
   .findFirst()
   .orElseThrow(() -> new NoSuchElementException(
     String.format("Option with text %s not found", value.toString()))).click();
}

代码示例来源:origin: Wikia/selenium-tests

private void typeNewFileName(String newFileName) {
 wait.forElementVisible(fileNameInput);
 if (Boolean.parseBoolean(fileNameInput.getAttribute("readonly"))) {
  throw new NoSuchElementException(
    "File name input is read only! This file already exist on this wiki");
 }
 fileNameInput.clear();
 fileNameInput.sendKeys(newFileName);
 waitForValueToBePresentInElementsAttributeByElement(fileNameInput, "value", newFileName);
}

代码示例来源:origin: jenkinsci/acceptance-test-harness

/**
 * Finds a pending method signature that includes the given text in the signature itself.
 */
public PendingSignature findSignature(String context) {
  for (WebElement e : all(by.xpath(".//div[starts-with(@id,'s-')]"))) {
    if (e.findElement(by.tagName("code")).getText().contains(context)) {
      return new PendingSignature(injector,e);
    }
  }
  throw new NoSuchElementException(context);
}

代码示例来源:origin: Wikia/selenium-tests

public void adjustPosition(PositionsVideo position) {
 wait.forElementVisible(videoEmbedLayotRow);
 switch (position) {
  case LEFT:
   positionLayoutLeft.click();
   break;
  case CENTER:
   positionLayoutCenter.click();
   break;
  case RIGHT:
   positionLayoutRight.click();
   break;
  default:
   throw new NoSuchElementException("Non-existing position selected");
 }
 Log.log("adjustPosition", "position " + position.toString() + " selected", true);
}

代码示例来源:origin: Wikia/selenium-tests

public String getImageUrl(String imageName) {
 for (WebElement elem : imagesNewFiles) {
  String href = elem.findElement(parentBy).getAttribute("href");
  if (href.contains(imageName)) {
   return href;
  }
 }
 throw new NoSuchElementException("there is no " + imageName + " on Special:NewFiles page");
}

代码示例来源:origin: jenkinsci/acceptance-test-harness

public <T extends View> T  selectView(final Class<T> type, final String viewName) {
  final List<WebElement> viewTabs = this.getViewTabs();
  int i = 0;
  for (final WebElement tab : viewTabs) {
    if (tab.getText().equals(viewName)) {
      tab.click();
      if (i == 0) {
        // First tab redirects to Folder's home page
        return null;
      } else {
        return newInstance(type, injector, url("view/%s/", viewName));
      }
    }
    i++;
  }
  throw new NoSuchElementException(String.format("There is no view with name [%s]", viewName));
}

代码示例来源:origin: com.github.bordertech.wcomponents/wcomponents-test-lib

/**
   * Get an option based on its visible text.
   *
   * @param optionText the text of the option we are trying to find
   * @return the option
   */
  public WebElement getOption(final String optionText) {
    if (optionText == null) {
      throw new SystemException("option text must not be null");
    }

    for (WebElement we : getOptions()) {
      if (optionText.equals(we.getText())) {
        return we;
      }
    }
    throw new NoSuchElementException("No option with text provided");
  }
}

代码示例来源:origin: Ardesco/Powder-Monkey

public String getToolTipLine(int lineNo) throws NoSuchElementException {
  List<String> lines = new ArrayList<String>();
  List<WebElement> toolTipLines = toolTip.findElements(By.cssSelector("text tspan"));
  for (WebElement toolTipLine : toolTipLines) {
    lines.add(toolTipLine.getText());
  }
  if (lineNo > lines.size()) {
    throw new NoSuchElementException("There is no line " + lineNo + "! There are only " + lines.size() + " lines in the tool tip");
  }
  //We return line - 1 because the lines Array starts a 0 not 1
  return lines.get(lineNo - 1);
}

代码示例来源:origin: yandex-qatools/htmlelements

/**
 * Selects first radio button that has a value matching the specified argument.
 *
 * @param value The value to match against.
 */
public void selectByValue(String value) {
  WebElement matchingButton = getButtons().stream()
      .filter(b -> value.equals(b.getAttribute("value")))
      .findFirst()
      .orElseThrow(() -> new NoSuchElementException(
          String.format("Cannot locate radio button with value: %s", value)));
  selectButton(matchingButton);
}

代码示例来源:origin: vmi/selenese-runner-java

break;
case "id":
  WebElement option = select.findElement(By.id(oLoc.arg));
  if (option.isSelected() ^ selectOrDeselect)
    option.click();
  break;
case "index":
String msg = e.getMessage();
int nlIndex = msg.indexOf('\n');
if (nlIndex > 0) {

代码示例来源:origin: windup/windup

private boolean isDependencyPathsExists(WebElement traitsElement, String key, final List<String> foundPaths, String dependencyName)
{
  String id = dependencyName + "-"+key;
  try
  {
    List<WebElement> pathElements = traitsElement.findElements(By.xpath("//ul[@id='" + id +"']/li"));
    if ( pathElements.size() != foundPaths.size())
    {
      return false;
    }
    List<String> pathsOnPage = new ArrayList<>();
    for (WebElement webElement : pathElements)
    {
       pathsOnPage.add(webElement.getText());
    }
    
    pathsOnPage.removeAll(foundPaths);
    if (pathsOnPage.isEmpty())
    {
      return true;
    }
  } 
  catch (org.openqa.selenium.NoSuchElementException e) 
  {
    System.err.println("Element not found " + e.getLocalizedMessage());
  }
  return false;
}

代码示例来源:origin: windup/windup

private boolean isDependencyPropertyExists(WebElement traitsElement, String key, String value, String dependencyName)
{
  String id = dependencyName +"-"+key;
  try
  {
    WebElement header = traitsElement.findElement(By.id(id));
    if (header != null && header.getText().endsWith(value))
    {
      return true;
    }
  }
  catch (org.openqa.selenium.NoSuchElementException e) 
  {
    System.err.println("Element not found " + e.getLocalizedMessage());
  }
  return false;
}

代码示例来源:origin: Wikia/selenium-tests

public void typeInParam(String paramName, String text) {
 waitForDialogVisible();
 if (isElementOnPage(TEMPLATE_PARAMS_BY)) {
  WebElement targetParam = Elements.getElementByChildText(templateParams,
                              PARAM_LABEL_BY,
                              paramName
  );
  WebElement targetParamInput = targetParam.findElement(PARAM_INPUT_BY);
  targetParamInput.sendKeys(text);
  waitForValueToBePresentInElementsAttributeByElement(targetParamInput, "value", text);
 } else {
  throw new NoSuchElementException("This template has no param.");
 }
 Log.log("typeInParam", "Type " + text + " in the " + paramName + " field.", true, driver);
}

代码示例来源:origin: cz.etnetera/seb

@Override
public void checkIfPresent(WebElement element) throws NoSuchElementException {
  if (element == null)
    throw new NoSuchElementException("Element is null");
  element.isDisplayed();
}

代码示例来源:origin: jenkinsci/acceptance-test-harness

private WebElement findElement(WebElement context, By selector) {
  try {
    return context.findElement(selector);
  } catch (NoSuchElementException x) {
    // this is often the best place to set a breakpoint
    String msg = String.format("Unable to locate %s in %s", selector, driver.getCurrentUrl());
    throw new NoSuchElementException(msg, x);
  }
}

代码示例来源:origin: org.seleniumhq.webdriver/webdriver-support

/**
 * @return The first selected option in this select tag (or the currently selected option in a
 *         normal select)
 */
public WebElement getFirstSelectedOption() {
 for (WebElement option : getOptions()) {
  if (option.isSelected()) {
   return option;
  }
 }
 throw new NoSuchElementException("No options are selected");
}

代码示例来源:origin: selenide/selenide

@Override
public Void execute(SelenideElement proxy, WebElementSource selectField, Object[] args) {
 String text = (String) args[0];
 WebElement element = selectField.getWebElement();
 Select select = new Select(element);
 List<WebElement> options = element.findElements(By.xpath(
   ".//option[contains(normalize-space(.), " + Quotes.escape(text) + ")]"));
 if (options.isEmpty()) {
  throw new NoSuchElementException("Cannot locate option containing text: " + text);
 }
 for (WebElement option : options) {
  setSelected(option);
  if (!select.isMultiple()) {
   break;
  }
 }
 return null;
}

代码示例来源:origin: org.jboss.arquillian.graphene/graphene-webdriver-impl

@Override
  public Object getTarget() {
    try {
      return dropProxyAndFindElement(by, searchContext);
    } catch (NoSuchElementException ex) {
      throw new NoSuchElementException((by instanceof ByIdOrName ? EMPTY_FIND_BY_WARNING : "") + ex.getMessage(),
        ex);
    }
  }
});

代码示例来源:origin: Wikia/selenium-tests

private boolean isElementVisible(WebElement element) {
 try {
  return element.isDisplayed();
 } catch (NoSuchElementException e) {
  Log.info(e.getMessage());
  return false;
 }
}

相关文章