如何从 C# Selenium 中的 IWebElement 获取 FindsByAttribute? [页面对象模型]

How to get FindsByAttribute from IWebElement in C# Selenium ? [Page Object Model]

鉴于 Selenium 中的页面对象模型设计模式,我需要在对其执行操作之前检查页面上的 WebElement 是否 present/enabled/clickable。

基础'Page'class:

 public class Page
{            
    public Page()
    {
        PageFactory.InitElements(SeleniumTests.driver, this);
    }  
}

继承class:

class Page_Certificate_ChooseOperator : Page
    {               
        [FindsBy(How = How.Id, Using = "SearchOperatorName")]
        public IWebElement txtName { get; set; }

        [FindsBy(How = How.Id, Using = "SearchOperatorEstablishmentNumber")]
        public IWebElement txtEstablishmentNumber { get; set; }

        [FindsBy(How = How.Id, Using = "searchButton")]
        public IWebElement btnSearch { get; set; }

        public void SelectOperator(String name, String establishmentNumber)
        {
            this.txtName.SetInputField(name);
            this.txtEstablishmentNumber.SetInputField(establishmentNumber);                
            this.btnSearch.SafeClick();
        }           
    }

最后 class 扩展方法:

 public static class SeleniumExtensionMethod
    {
        public static void SetInputField(this IWebElement webElement, String value)
        {
            webElement.Clear();
            webElement.SendKeys(value);
        }    

        public static void SafeClick(this IWebElement webElement, int timeout_in_seconds)
        {     
            //This is the difficult part. I need to check if the webElement is visible, but I don't want to write "low-level" code and specify the ID and  selector. Is there a way I can find out how the webElement was created by this class and say something like "webElement.How" to find "How.ID" or "webElement.Using" to find "btnSearch"? I need to use something like the below code using the PageObject
            // WebDriverWait wait = new WebDriverWait(SeleniumTests.driver, TimeSpan.FromSeconds(10));
            // IWebElement dynamicElement = wait.Until<IWebElement>(driver => driver.FindElement(By.Id("btnSearch")));  
            // dynamicElement.Click();

            //Now, I just use this, but it crashes on NoSuchElementFound since it goes too fast. I don't want to use ImplicitWait.
            btnSearch.Click();


        }
    }

我面临的挑战是在单击按钮 btnSearch 之前我需要一个 ExplicitWaity,因为它会抛出 NoSuchElementFoundException。由于我使用 [FindsBy] 属性,我想应该有一些方法可以检查 webElement 是如何 found/created?我怎样才能做到这一点?或者我如何拥有简洁的代码并在页面对象上使用 ExplicitWait?

要等待元素成为 present/enabled/clickable,您可以使用具有 ElementToBeClickable 条件的服务员:

[FindsBy(How = How.Id, Using = "searchButton")]
public IWebElement btnSearch { get; set; }

public void SelectOperator(String name, String establishmentNumber)
{
    this.txtName.SetInputField(name);
    this.txtEstablishmentNumber.SetInputField(establishmentNumber);                

    new WebDriverWait(SeleniumTests.driver, TimeSpan.FromSeconds(10))
        .Until(ExpectedConditions.ElementToBeClickable(this.btnSearch))
        .Click();
}