Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从C#Selenium中的IWebElement获取FindByAttribute?[页面对象模型]_C#_Selenium_Wait_Pageobjects - Fatal编程技术网

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

如何从C#Selenium中的IWebElement获取FindByAttribute?[页面对象模型],c#,selenium,wait,pageobjects,C#,Selenium,Wait,Pageobjects,给定Selenium中的页面对象模型设计模式,在对其执行操作之前,我需要检查页面上的WebElement是否存在/启用/可单击 基本“页面”类: public class Page { public Page() { PageFactory.InitElements(SeleniumTests.driver, this); } } 继承的类: class Page_Certificate_ChooseOperator : P

给定Selenium中的页面对象模型设计模式,在对其执行操作之前,我需要检查页面上的WebElement是否存在/启用/可单击

基本“页面”类:

 public class Page
{            
    public Page()
    {
        PageFactory.InitElements(SeleniumTests.driver, this);
    }  
}
继承的类:

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();
        }           
    }
最后是扩展方法的类:

 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();


        }
    }
公共静态类SeleniumExtensionMethod
{
公共静态void SetInputField(此IWebElement webElement,字符串值)
{
webElement.Clear();
webElement.SendKeys(值);
}    
公共静态void SafeClick(此IWebElement webElement,int timeout,以秒为单位)
{     
//这是困难的部分。我需要检查webElement是否可见,但我不想编写“低级”代码并指定ID和选择器。有没有办法让我知道webElement是如何由此类创建的,比如说“webElement.how”来查找“how.ID”或“webElement.Using”来查找“btnSearch”?我需要使用PageObject使用下面的代码
//WebDriverWait wait=新的WebDriverWait(SeleniumTests.driver,TimeSpan.FromSeconds(10));
//IWebElement dynamicElement=wait.Until(driver=>driver.FindElement(By.Id(“btnSearch”));
//dynamicElement.Click();
//现在,我只使用了这个,但是它在NoTouchElementFound上崩溃了,因为它运行得太快了。我不想使用隐式等待。
btnSearch.Click();
}
}

我面临的挑战是,在单击按钮
btnSearch
之前,我需要一个明确的方法,因为它会抛出一个NoTouchElementFoundException。由于我使用了[FindsBy]属性,我想应该有某种方法来检查webElement是如何找到/创建的?我该怎么做?或者我如何拥有整洁的代码并在页面对象上使用明确的twait?

要等待元素出现/启用/可点击,您可以使用带有
元素可点击条件的服务员:

[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();
}

这有时会起作用,但有时也会引发WebDriver.Support.dll中发生的TargetInvocationException。你知道怎么解决这个问题吗?这对我们项目的成功非常重要。谢谢什么是内在的例外?你应该提出一个新问题,这似乎是一个不同的问题。