代码之家  ›  专栏  ›  技术社区  ›  SeekanDestroy

Selenium:将WebElements添加到列表中,然后检查所有元素的可见性。

  •  1
  • SeekanDestroy  · 技术社区  · 6 年前

    使用Selenium和Cucumber,我试图建立一种通过args存储WebElement的方法。然后使用第二个方法,我将尝试检查列表中所有元素的可见性。

    我有一些元素是这样定位的:

     @FindBy(how = How.XPATH, using = "//* 
     [@id=\"content\"]/section/fieldset/form/div[1]/div[2]/input")
     public WebElement formName;
     @FindBy(how = How.CSS, using = "//* 
     [@id=\"content\"]/section/fieldset/form/div[2]/div[2]/input")
     public WebElement formPassword;
     @FindBy(how = How.ID, using = "remember")
     public WebElement rememberMe;
     @FindBy(how = How.CLASS_NAME, using = "button principal")
     public WebElement loginButton;
    

    然后我编写了这个方法来在列表中添加WebElements:

    public void crearListaWebElement(WebElement... elements){
        List<WebElement> webElementList = new ArrayList<>();
        for (WebElement element : elements){
            webElementList.add(elements);
        }
    }
    

    在上获取此错误。添加(元素)1问题:

    添加 (Java.UTI.Cube) 无法应用In-List 到 (org.openqa.selenium.webelement[])

    无法理解为什么我不能将这些相同类型的元素添加到我的列表中

    然后,下面是第二种检查可见性的方法:

    public void estaVisible(WebDriver(tried WebElement as well) visibleWebElements) {
    
        boolean esvisible =  driver.findElement(visibleWebElements).isDisplayed();
        Assert.assertTrue(esvisible);
        return esvisible;
    }
    

    在“visibleWebElement”上获取此错误:

    WebDriver中的findElement(org.openqa.selenium.by)不能应用于 (org.openqa.selenium.webdriver)

    我理解这两种不同类型的对象之间的冲突,但是,WebDriver发现的对象不是存储为WebElements吗?

    有人能在这放点灯吗? 事先谢谢。 γ

    3 回复  |  直到 6 年前
        1
  •  1
  •   Shivam Mishra    6 年前

    无法理解为什么我不能将这些相同类型的元素添加到我的列表中-

    你的代码中很可能有错别字。应该是 element 而不是 elements :

    for (WebElement element : elements){
            webElementList.add(element);
        }
    

    在“visibleWebElement”上获取此错误-

    driver.findElement() 方法接受 By 对象作为参数,它是选择器。但是你路过了 visibleWebElements 这是一个 WebElement 对象,这就是代码出错的原因。对于ex,如果您希望通过xpath定位元素,这是正确的使用方法:

    WebElement your_element = driver.findElement(By.xpath("//whatever xpath"));
    

    您可以直接使用 isDisplayed() WebElement上的方法:

    public void estaVisible(WebElement visibleWebElements) {
    
        boolean esvisible =  visibleWebElements.isDisplayed();
        Assert.assertTrue(esvisible);
        return esvisible;
    }
    
        2
  •  1
  •   Ashok kumar Ganesan    6 年前

    当您试图检查Web元素列表时,必须检查所有元素的可见性。请尝试使用以下代码

    WebDriver等待Web元素列表

    将所有元素存储在列表中

    List<WebElement> listOfAllWebElements = new ArrayList<WebElement>();
    listOfAllWebElements.add(elementOne);
    listOfAllWebElements.add(elementTwo);
    listOfAllWebElements.add(elementThree);
    
    WebDriverWait listWait = new WebDriverWait(driver,10);
    listWait.until(ExpectedConditions.visibilityOfAllElements(listOfAllWebElements));
    

    WebDriver等待单个Web元素

    WebElement element = driver.findElement(By.id("element"));
    
    WebDriverWait wait = new WebDriverWait(driver,10);
    wait.until(ExpectedConditions.visibilityOf(element));
    
        3
  •  0
  •   JJJ Sergey    6 年前

    此函数将返回一个布尔值,并检查列表元素是否可见。

    public boolean isListElementsVisible(WebDriver driver, By locator) {
        boolean result = false;
        try {
            WebDriverWait wait = new WebDriverWait(driver, 30);
            wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(locator));
            result = true;
        } catch (Exception e) {
            System.out.println(e.getMessage());
            result = false;
        }
       return result;
    }