在Selenium Web驱动程序2.44.0中遇到Wait until element present功能的问题

时间:2015-06-29 08:19:39

标签: firefox selenium selenium-webdriver automation webdriver

我遇到了元素等待/存在/可点击的随机行为问题。我已经使用下面的逻辑来等待那个元素,但是它的工作有些时候并且在其他时候没有工作。有人可以帮我解决这个问题,用通用/标准的元素等待方式来解决这个问题。

问题/要求:在加载网页时,我必须等待一个元素直到它的当前/可点击。我正在使用Selenium Web驱动程序2.44.0和Firefox 33.0.3

使用的逻辑:

package com.ericsson.testing.automation.framework.ui.common;

import java.util.NoSuchElementException;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.ElementNotVisibleException;
import org.openqa.selenium.StaleElementReferenceException;
import org.openqa.selenium.UnhandledAlertException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.WebDriverWait;

import com.google.common.base.Function;

public class Test {
    public static WebDriver driver;
    public static long timeOut = 180000;
    /** The poll time. */
    public int pollTime = 100;

    public void isElementClickable(WebDriver driver, WebElement webelement,
            long timeOutForEachElement) {
        try {
            WebDriverWait wait = new WebDriverWait(driver,
                    timeOutForEachElement);
            wait.until(ExpectedConditions.elementToBeClickable(webelement));
        } catch (UnhandledAlertException ex) {
            // Some debug logging info
        }
    }

    // Senario 2
    public void waitTillElementPresent(WebElement webelement) {
        System.out.println("Before Fluent Wait");
        System.out.println("Xpath = " + webelement);
        FluentWait<WebElement> fluentWait = new FluentWait<WebElement>(
                webelement);
        fluentWait.pollingEvery(pollTime, TimeUnit.MILLISECONDS);
        fluentWait.withTimeout(timeOut, TimeUnit.MILLISECONDS);
        System.out.println("After Fluent Wait");
        fluentWait.until(new Function<WebElement, Boolean>() {

            public Boolean apply(WebElement webelement) {
                try {
                    System.out.println("inside isDisplayed check");
                    return webelement.isDisplayed();
                } catch (NoSuchElementException ex) {
                    System.out.println("Inside NoSuchElementException");
                    return false;
                } catch (ElementNotVisibleException ex) {
                    System.out.println("Inside ElementNotVisibleException");
                    return false;
                } catch (StaleElementReferenceException ex) {
                    System.out.println("Inside StaleElementRefException");
                    return false;
                } catch (UnhandledAlertException ex) {
                    System.out.println("Inside UnhandledAlertException");
                    // Some logic for debug logging
                    return false;
                }
            }
        });
    }

    // Senario 3
    public void clickOnElement(String element) {
        while (true) {
            driver.findElement(By.xpath(element)).click();
            System.out.println("Trying to click on element" + element);
            break;
        }
        System.out.println("Clicked on element" + element);
    }

    /*
     * private boolean isElementPresent(String element) {
     *                  int myLink
     *  =driver.findElements(By.xpath(element)).size();
     *                  if (myLink != 0)                      return true;
     *                  else                      return false;              }
     */

    // Senario 4
    public void waitTillElementisClicked(String element) {
        boolean flag = true;
        while (flag == true) {
            // driver.findElement(By.xpath(element)).click();
            // flag=driver.findElement(By.xpath(element)).isSelected();
            driver.findElement(By.xpath(element)).click();
            // driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
            if (driver.findElement(By.xpath(element)).isSelected()) {
                flag = false;
                System.out.println("element already clicked");
            }

            // driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
            System.out.println("trying to click the element");
        }
        System.out.println("Clicking the element");
    }
}

3 个答案:

答案 0 :(得分:0)

尝试“ driver.findElement(By.xpath(element))。size()&gt; 0 ”。根据我的经验,如果你正在做一切正确仍然面临元素问题,这是有效的。

答案 1 :(得分:0)

Though the size() works for webelements, you could pass your single element in the locator
wait.until(new ExpectedCondition<Boolean>() 
        {
            public Boolean apply(WebDriver webDriver) 
            {
                return verifyWebElementListIsPresent(se.driver().findElements(locator));
            }
        });   
 public boolean verifyWebElementListIsPresent(List<WebElement> element)
    {
        List<WebElement> adminPermissions = element;
        if (adminPermissions.size() > 0)
        {
            System.out.println("Element is displayed on the page");
            return true;    
        }
        else
        {
            System.out.println("Element is not displayed on the page");
            return false;
        }  
    }

答案 2 :(得分:0)

public static WebElement elementExists(String xpath){
        // make sure driver is available to this method..
        WebDriverWait wait = new WebDriverWait(driver, 2); //Increase wait time if required                                                                               
        try {
            wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(xpath)));
            System.out.println("Element having xpath - "+xpath+" is present!");
            return driver.findElement(By.xpath(xpath));  
        }catch(ElementNotFoundException e1){
            System.out.println("Element having xpath - "+xpath+" is not present!");
            return null;
        } catch (TimeoutException e) {
            System.out.println("Element having xpath - "+xpath+" is not present!");
            return null;
        }
    }
  • WebElement xyz = elementExists(传递xpath字符串);

  • 如果找到web元素,它将返回web元素,否则返回null。

  • 如果存在网络元素,请检查是否显示以供使用。 webelement.isDisplayed()应该返回true。