【问题标题】:Having an issue with Wait till element present functionality in Selenium Web driver 2.44.0Selenium Web 驱动程序 2.44.0 中的等待元素存在功能存在问题
【发布时间】:2015-09-15 15:13:33
【问题描述】:

我面临元素等待/呈现/可点击的随机行为问题。我已经使用下面的逻辑来等待该元素,但它有时会起作用,而其他时候则不起作用。有人可以帮助我如何用元素等待的通用/标准方式解决这个问题。

问题/要求:加载网页时,我必须等待一个元素出现/可点击。我正在使用 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");
    }
}

【问题讨论】:

    标签: firefox selenium selenium-webdriver automation webdriver


    【解决方案1】:

    尝试“driver.findElement(By.xpath(element)).size()>0”。以我的经验,如果你做的一切都是正确的,但仍然面临元素问题,这是可行的。

    【讨论】:

    • 您能否提供有关该场景的更多信息?
    • 您的意思可能是.findElements(),因为对单个元素的.size() 调用会引发错误。
    • 在这种情况下,我无法将 webelement 转换为字符串。如何将此 webelement 转换为字符串(错误:类型 By 中的方法 xpath(String) 不适用于参数 (WebElement))。 if(driver.findElement(By.xpath(homePage.homeButton())).size()>0){ homePage.homeButton().click(); }
    • 您能否提供有关该场景的更多信息? ——阿尔宾·约瑟夫。
    • @AlbinJoseph-您能否提供有关该场景的更多信息? – 我正在等待页面中的元素。取决于页面的加载,可能需要 2 到 50 秒。但我不能每次都隐含地等到 50 秒。所以我想写一个方法等到元素加载/呈现/可点击。正如我在问题中提到的,我使用了一些场景,但它不适用于我的 Selenium 版本(2.44.0)。想知道“是否有任何通用 API 方法可以解决我的问题?”或者这是我的硒版本的问题
    【解决方案2】:
    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;
            }  
        }
    

    【讨论】:

      【解决方案3】:
      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 element,则返回web element,否则返回null。

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

      【讨论】:

        猜你喜欢
        • 2012-02-28
        • 1970-01-01
        • 2016-09-28
        • 2020-03-04
        • 1970-01-01
        • 2016-12-25
        • 1970-01-01
        • 1970-01-01
        • 2019-04-05
        相关资源
        最近更新 更多