首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >查找一个元素是否存在于另一个元素Selenium中

问查找一个元素是否存在于另一个元素Selenium中
EN

Stack Overflow用户
提问于 2020-01-31 19:38:11
回答 1查看 17关注 0票数 0

​

​

我想看看数字65692后面有没有'+‘号。

我试过了:

代码语言:javascript
复制
driver.findElement(By.id(activeResource + "Current_Works_Order")).findElements(By.className("fa fa-plus multipleOpsIcon"));

即使没有'+‘,这也会返回一个列表。

搜索此元素的最佳方法是什么?

EN

回答 1

Stack Overflow用户

发布于 2020-01-31 21:46:17

如果你不想接收一个列表,并且只想接收一个元素,你应该使用"findElement“而不是"findElements”。

在我看来,您查找子元素的方法是有效的。

我用来检查元素是否存在的方法是:

代码语言:javascript
复制
public static boolean checkElement(By by) {
  try {

    if (!isPresent(by)) {
      return false;
    }

    if (!isVisible(by)) {
      return false;
    }

    return true;

  } catch (StaleElementReferenceException e) {

    e.printStackTrace();
    return false;

  } catch (Exception e) {
    return false;
  }
}

public static boolean isPresent(By by) {
  try {

    getElement(by).getLocation();
    return true;

  } catch (StaleElementReferenceException e) {

    e.printStackTrace();
    return false;

  } catch (Exception e) {
    return false;
  }
}

public static boolean isVisible(By by) {
  try {

    return getElement(by).isDisplayed();

  } catch (StaleElementReferenceException e) {

    e.printStackTrace();
    return false;

  } catch (Exception e) {
    return false;
  }
}

public static WebElement getElement(By by) {
  try {

    return driver.findElement(by);

  } catch (StaleElementReferenceException e) {

    e.printStackTrace();
    return null;

  } catch (Exception e) {
    return null;
  }
}

希望能有所帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60003185

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档