首页 文章

Selenium WebDriver - 测试元素是否存在

提问于
浏览
134

有没有办法测试元素是否存在?任何findElement方法都会以异常结束,但这不是我想要的,因为它可能是一个元素不存在而且没关系,这不是测试的失败,因此异常不能成为解决方案 .

我发现这篇文章:Selenium c# Webdriver: Wait Until Element is Present但这是针对C#而我并不是很擅长 . 任何人都可以将代码翻译成Java吗?我很抱歉,我在Eclipse中尝试过但是我没有把它直接用于Java代码 .

这是代码:

public static class WebDriverExtensions{
    public static IWebElement FindElement(this IWebDriver driver, By by, int timeoutInSeconds){

        if (timeoutInSeconds > 0){
            var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutInSeconds));
            return wait.Until(drv => drv.FindElement(by));
        }

        return driver.FindElement(by);
    }
}

20 回答

  • 0

    使用 findElements 而不是 findElement .

    如果找不到匹配的元素而不是异常, findElements 将返回一个空列表 .

    要检查元素是否存在,您可以尝试这样做

    Boolean isPresent = driver.findElements(By.yourLocator).size() > 0
    

    如果找到至少一个元素,则返回true,如果不存在则返回false .

  • 0

    那个简单地查找元素并确定它是否存在的私有方法怎么样:

    private boolean existsElement(String id) {
        try {
            driver.findElement(By.id(id));
        } catch (NoSuchElementException e) {
            return false;
        }
        return true;
    }
    

    这将非常容易,并且可以完成工作 .

    编辑:你甚至可以进一步采用 By elementLocator 作为参数,如果你想通过id以外的东西找到元素,就可以解决问题 .

  • 47

    我发现这适用于Java:

    WebDriverWait waiter = new WebDriverWait(driver, 5000);
    waiter.until( ExpectedConditions.presenceOfElementLocated(by) );
    driver.FindElement(by);
    
  • 0
    public static WebElement FindElement(WebDriver driver, By by, int timeoutInSeconds)
    {
        WebDriverWait wait = new WebDriverWait(driver, timeoutInSeconds);
        wait.until( ExpectedConditions.presenceOfElementLocated(by) ); //throws a timeout exception if element not present after waiting <timeoutInSeconds> seconds
        return driver.findElement(by);
    }
    
  • 6

    我遇到过同样的问题 . 对我来说,根据用户的权限级别,页面上不会显示某些链接,按钮和其他元素 . 我的套件的一部分是测试缺少应该丢失的元素 . 我花了好几个小时试图解决这个问题 . 我终于找到了完美的解决方案 .

    这样做,告诉浏览器查找指定的任何和所有元素 . 如果它导致 0 ,则表示未找到基于规范的元素 . 然后我让代码执行一个if语句让我知道它没有找到 .

    这是在 C# ,因此需要对 Java 进行翻译 . 但不应该太难 .

    public void verifyPermission(string link)
    {
        IList<IWebElement> adminPermissions = driver.FindElements(By.CssSelector(link));
        if (adminPermissions.Count == 0)
        {
            Console.WriteLine("User's permission properly hidden");
        }
    }
    

    根据您的测试需要,您还可以选择其他路径 .

    以下代码段正在检查页面上是否存在非常特定的元素 . 根据元素的存在,我有测试执行if else .

    如果该元素存在并显示在页面上,我有 console.write 让我知道并继续前进 . 如果有问题的元素存在,我无法执行我需要的测试,这是需要设置它的主要原因 .

    如果元素不存在,并且未显示在页面上 . 我在else中执行了测试 .

    IList<IWebElement> deviceNotFound = driver.FindElements(By.CssSelector("CSS LINK GOES HERE"));
    //if the element specified above results in more than 0 elements and is displayed on page execute the following, otherwise execute whats in the else statement
    if (deviceNotFound.Count > 0 && deviceNotFound[0].Displayed){
        //script to execute if element is found
    } else {
        //Test script goes here.
    }
    

    我知道我对OP的回应有点迟了 . 希望这有助于某人!

  • 3

    试试这个:调用这个方法并传递3个参数:

    • WebDriver变量 . //假设driver_variable为驱动程序 .

    • 您要检查的元素 . 应该从By方法提供 . // ex:By.id("id")

    • 以秒为单位的时间限制 .

    示例:waitForElementPresent(driver,By.id(“id”),10);

    public static WebElement waitForElementPresent(WebDriver driver, final By by, int timeOutInSeconds) {
    
            WebElement element; 
    
            try{
                driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS); //nullify implicitlyWait() 
    
                WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds); 
                element = wait.until(ExpectedConditions.presenceOfElementLocated(by));
    
                driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); //reset implicitlyWait
                return element; //return the element
            } catch (Exception e) {
                e.printStackTrace();
            } 
            return null; 
        }
    
  • 5

    您可以通过在try catch语句之前缩短selenium超时来使代码运行得更快 .

    我使用以下代码来检查元素是否存在 .

    protected boolean isElementPresent(By selector) {
        selenium.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
        logger.debug("Is element present"+selector);
        boolean returnVal = true;
        try{
            selenium.findElement(selector);
        } catch (NoSuchElementException e){
            returnVal = false;
        } finally {
            selenium.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
        }
        return returnVal;
    }
    
  • 0

    使用Java编写以下函数/方法:

    protected boolean isElementPresent(By by){
            try{
                driver.findElement(by);
                return true;
            }
            catch(NoSuchElementException e){
                return false;
            }
        }
    

    在断言期间使用适当的参数调用方法 .

  • 1

    如果你在ruby中使用rspec-Webdriver,你可以使用这个脚本,假设一个元素应该真的不存在而且它是一个通过的测试 .

    首先,首先从类RB文件中编写此方法

    class Test
     def element_present?
        begin
            browser.find_element(:name, "this_element_id".displayed?
            rescue Selenium::WebDriver::Error::NoSuchElementError
                puts "this element should not be present"
            end
     end
    

    然后,在您的spec文件上,调用该方法 .

    before(:all) do    
        @Test= Test.new(@browser)
      end
    
     @Test.element_present?.should == nil
    

    如果您的元素不存在,您的规范将通过,但如果元素存在,则会抛出错误,测试失败 .

  • 5

    这对我有用:

    if(!driver.findElements(By.xpath("//*[@id='submit']")).isEmpty()){
        //THEN CLICK ON THE SUBMIT BUTTON
    }else{
        //DO SOMETHING ELSE AS SUBMIT BUTTON IS NOT THERE
    }
    
  • 12

    要查找特定元素是否存在,我们必须使用findElements()方法而不是findElement() .

    int i=driver.findElements(By.xpath(".......")).size();
    if(i=0)
    System.out.println("Element is not present");
    else
    System.out.println("Element is present");
    

    这对我有用..建议我,如果我错了..

  • 0

    这应该这样做:

    try {
        driver.findElement(By.id(id));
    } catch (NoSuchElementException e) {
        //do what you need here if you were expecting
        //the element wouldn't exist
    }
    
  • 0

    提供我的代码片段 . 因此,以下方法检查页面上是否存在随机Web元素“ Create New Application ”按钮 . 请注意,我已将等待时间用作 0 秒 .

    public boolean isCreateNewApplicationButtonVisible(){
        WebDriverWait zeroWait = new WebDriverWait(driver, 0);
        ExpectedCondition<WebElement> c = ExpectedConditions.presenceOfElementLocated(By.xpath("//input[@value='Create New Application']"));
        try {
            zeroWait.until(c);
            logger.debug("Create New Application button is visible");
            return true;
        } catch (TimeoutException e) {
            logger.debug("Create New Application button is not visible");
            return false;
        }
    }
    
  • -1

    我会使用类似的东西(使用Scala [旧的代码“好”Java 8可能与此类似]):

    object SeleniumFacade {
    
      def getElement(bySelector: By, maybeParent: Option[WebElement] = None, withIndex: Int = 0)(implicit driver: RemoteWebDriver): Option[WebElement] = {
        val elements = maybeParent match {
          case Some(parent) => parent.findElements(bySelector).asScala
          case None => driver.findElements(bySelector).asScala
        }
        if (elements.nonEmpty) {
          Try { Some(elements(withIndex)) } getOrElse None
        } else None
      }
      ...
    }
    

    那么,

    val maybeHeaderLink = SeleniumFacade getElement(By.xpath(".//a"), Some(someParentElement))
    
  • 1

    我在Java中找到的最简单的方法是:

    List<WebElement> linkSearch=  driver.findElements(By.id("linkTag"));
    int checkLink=linkSearch.size();
    if(checkLink!=0){  //do something you want}
    
  • -1

    您可以尝试隐式等待:

    WebDriver driver = new FirefoxDriver();
    driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
    driver.Url = "http://somedomain/url_that_delays_loading";
    IWebElement myDynamicElement = driver.FindElement(By.Id("someDynamicElement"));
    

    `

    或者您可以尝试显式等待:`

    IWebDriver driver = new FirefoxDriver();
    driver.Url = "http://somedomain/url_that_delays_loading";
    WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
    IWebElement myDynamicElement = wait.Until<IWebElement>((d) =>
        {
            return d.FindElement(By.Id("someDynamicElement"));
        });
    

    `

    显式将在某个操作之前检查元素是否存在 . 可以在代码中的每个位置调用隐式等待 . 例如,在一些AJAX操作之后 .

    更多你可以在SeleniumHQ page找到

  • 3
    public boolean isElementFound( String text) {
            try{
    
                WebElement webElement = appiumDriver.findElement(By.xpath(text));
                System.out.println("isElementFound : true :"+text + "true");
    
            }catch(NoSuchElementException e){
                System.out.println("isElementFound : false :"+text);
                return false;
            }
            return true;
        }
    
  • -2
    public boolean isElementDisplayed() {
            return !driver.findElements(By.xpath("...")).isEmpty();
        }
    
  • -1

    以下是检查Web元素 isPresentnot 的方法:

    我使用 XPath 作为元素 Identifier/Locator ,但您也可以使用其他定位器 .

    Solution I :

    public boolean isElementPresent(String xpathOfElement){
        try{
            driver.findElement(By.xpath(xpathOfElement));
            return true;
        }
        catch(NoSuchElementException e){
            return false;
        }
    }
    

    Solution II :

    public boolean isElementPresent(String xpathOfElement){
        boolean isPresent = false;
        if(!driver.findElements(By.xpath(xpathOfElement)).isEmpty()){
            isPresent=true;
        }
        return isPresent;
    }
    

    Solution III :

    public boolean isElementPresent(String xpathOfElement){
        return driver.findElements(By.xpath(xpathOfElement)).size() > 0;
    }
    
  • 227

    就个人而言,我总是把上述答案混合在一起创建一个可重用的静态实用程序方法,该方法使用size()<0建议:

    public Class Utility {
       ...
       public static boolean isElementExist(WebDriver driver, By by) {
          return driver.findElements(by).size() < 0;
       ...
    }
    

    这是整洁,可重复使用,可维护......所有那些好东西;-)

相关问题