WebDriver: How to check if an page object web element exists?

后端 未结 8 1674
鱼传尺愫
鱼传尺愫 2020-12-25 14:13

How to check if an Element exists, when using Page Objects with webdriver.

So far I am doing it this way.

DefaultPage defaultPage = PageFactory.initE         


        
8条回答
  •  轮回少年
    2020-12-25 14:40

    Using C# bindings:

    using System.Collections.Generic;
    using System.Linq;
    
    public class DefaultPage 
    {
        [FindsBy(How = How.Id, Using = "link_i_user_create")]
        private IList userCreateMenuLink;
    
        public bool isUserCreateMenuLinkPresent()
        {
            return userCreateMenuLink.Any();
        }
    }
    

    You're telling Selenium to grab all elements that match that Id and put them into a List of IWebElement. You then call .Any() on the list which evaluates to true if at least one IWebElement was found.

提交回复
热议问题