Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.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
Selenium C#使用PageObject检查IWebElement是否存在_C#_Selenium_Automation_Pageobjects - Fatal编程技术网

Selenium C#使用PageObject检查IWebElement是否存在

Selenium C#使用PageObject检查IWebElement是否存在,c#,selenium,automation,pageobjects,C#,Selenium,Automation,Pageobjects,目标:使用PageObjects检查当前页面上是否存在元素/IWebElement 我知道您可以使用以下内容: IWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(3)); IWebElement element = wait.Until(ExpectedConditions.ElementExists(By.Id("foo"))); 但是,当我使用PageObjects时,我不想再次使用id/xpath等。我目前正在使用下面

目标:使用PageObjects检查当前页面上是否存在元素/IWebElement

我知道您可以使用以下内容:

IWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(3));
IWebElement element = wait.Until(ExpectedConditions.ElementExists(By.Id("foo")));
但是,当我使用PageObjects时,我不想再次使用id/xpath等。我目前正在使用下面的方法检查元素是否存在。但是为了快速完成这个任务,我首先设置隐式等待,然后将其重置为默认值。它很好用,但感觉脏

我找到了其他一些老帖子。但这并没有为我提供任何解决方案。希望你能帮忙

页面对象:

        [FindsBy(How = How.Id, Using = "errorMessage")]
    public IWebElement btnSubmit { get; set; }
调用该方法:

CheckElementExists(errorMessage))
方法

public bool CheckElementExists(IWebElement pageObject)
    {
        Browser.getDriver.Manage().Timeouts().ImplicitWait = TimeSpan.FromMilliseconds(100);
        try
        {
            return pageObject.Equals(pageObject);
        }
        catch (NoSuchElementException)
        {
            return false;
        }
        finally
        {
            Browser.getDriver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(30);
        }
    }

我对C没有经验,但我认为它与JAVA非常相似,因此可以用C转换以下JAVA代码

若你们想写函数只是为了检查元素是否存在,这就是你们可以做的

public boolean isElementExisit(WebElement element){
    try{
        element.getTagName();
        return true;
    }catch (NoSuchElementException){
        return false;
    }
}
如果你想写一些需要等待的东西,那么你可以使用fluent wait

public void waitUntilElementIsPresent(WebElement element, int timeout){
        Wait wait = new FluentWait<WebDriver>(driver)
                .withTimeout(timeout, TimeUnit.SECONDS)
                .pollingEvery(5, TimeUnit.SECONDS)
                .ignoring(NoSuchElementException.class);

        wait.until(new Function() {
            @Override
            public Boolean apply(Object o) {
                element.getTagName();
                return true;
            }
        });
    }
public void waitUntilElementIsPresent(WebElement元素,int timeout){
等待等待=新建FluentWait(驱动程序)
.withTimeout(超时,时间单位为秒)
.轮询间隔(5,时间单位。秒)
.忽略(NoSuchElementException.class);
wait.until(新函数(){
@凌驾
公共布尔应用(对象o){
元素getTagName();
返回true;
}
});
}

您应该能够根据要使用的
id
在try中执行
FindElement
。如果找到它,则它将继续返回
true
,但如果它捕获了
NoTouchElementException
,则它将返回
false

bool CheckIfItExists(string ElementId)
{
    try
    {
        driver.FindElement(By.Id(ElementId));
        return true;
    }
    catch (NoSuchElementException)
    {
        return false;
    }
}

要断言元素的存在,使用C#和

要断言缺少元素,我非常喜欢语法

Assert.Throws(
()=>btnSubmit.Contains(“”),
“访问提交按钮应引发NosTouchElementException”
);

我认为您的解决方案是标准解决方案。问题是您应该等待一个元素(不应该存在)多长/短时间?没有标准答案,但它肯定是执行时间和测试可靠性之间的折衷。谢谢你的回复。我现在将隐式等待设置为0秒。并在任何地方使用显式等待。所以它现在工作得很好。谢谢你的回复。我也考虑过这一点,但我并不仅仅使用ID,对于一些页面对象,我使用XPath或类名。谢谢。我要去看看这个,等等!您的java解决方案看起来与我的C#解决方案非常相似:D
Assert.IsTrue(
  btnSubmit.Displayed,
  "Submit button should be displayed"
);
Assert.Throws<NoSuchElementException>(
  () => btnSubmit.Contains(""),
  "Accessing Submit button should throw NoSuchElementException"
);