简体   繁体   English

如何用C#在Selenium Locator中转换Selenium IWebElement?

[英]How to convert Selenium IWebElement in Selenium Locator By C#?

How to convert IWebElement in Locator Selenium WebDriver Page Objects? 如何在定位器Selenium WebDriver页面对象中转换IWebElement?

I work like this: 我这样工作:

using OpenQA.Selenium.Support.PageObjects;
        [FindsBy(How = How.Id, Using = "user")]
        public IWebElement txtUser { get; set; }

        public void fillUserField(string user)
        {
           wait.Until(ExpectedConditions.ElementIsVisible(By.Id("user")));
           txtUser.SendKeys(user);
        }

I do not want to repeat the ID "user" in wait. 我不想在等待中重复ID“用户”。

I don't work like this: 我不是这样的:

    public void fillUserField(string user)
    {   // TO DO - Convert IWebElement in Locator (BY)
        //Argument1: Cannot convert from 'OpenQA.Selenium.IWebElement' to 'OpenQA.Selenium.By'  
        wait.Until(ExpectedConditions.**ElementIsVisible(txtUser)**);
        txtUser.SendKeys(user);
    }

Is possible? 有可能吗 Thanks! 谢谢!

You would declare a locator at the top of the class. 您将在类的顶部声明一个定位器。

public By userLocator = By.Id("user");

and then use it like 然后像

wait.Until(ExpectedConditions.ElementIsVisible(userLocator));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM