简体   繁体   English

C#:等待元素在 Selenium 中包含特定属性

[英]C#: Wait for element to contain specific attribute in Selenium

I am waiting for an element Attribute aria-sort to equal " descending ".我正在等待元素属性aria-sort等于“ descending ”。

Currently using C# Selenium webdriver.目前使用 C# Selenium webdriver。 How do I conduct this?我该如何进行? I do not see AttributeContains in Visual Studio.我在 Visual Studio 中看不到 AttributeContains。

How to wait for a element to contain a specific attribute through Selenium and WebDriverWait? 如何通过 Selenium 和 WebDriverWait 等待元素包含特定属性?

boolean status = new WebDriverWait(driver, 20).until(ExpectedConditions.attributeContains(By.xpath("//div[@class='model-holder']/span[contains(.,'200K')]"), "class", "model-ready"));

Additionally to the way presented in the post you have mentioned in your question you can also do the following:除了您在问题中提到的帖子中介绍的方式之外,您还可以执行以下操作:
Let's say that element can be uniquely located by the following XPath //tag[@class='theClass']假设元素可以通过以下 XPath //tag[@class='theClass']唯一定位
So when this element will have aria-sort attribute equals to descending , then this element can be located by this XPath: //tag[@class='theClass' and(@aria-sort='descending')]所以当这个元素的aria-sort属性等于descending时,这个元素可以通过这个 XPath: //tag[@class='theClass' and(@aria-sort='descending')]
So you can simply use regular ElementExists ExpectedConditions as following:因此,您可以简单地使用常规ElementExists ExpectedConditions ,如下所示:

boolean status = new WebDriverWait(driver, 20).until(ExpectedConditions.ElementExists(By.xpath("//tag[@class='theClass' and(@aria-sort='descending')]"));

However this approach is not really good since it is not general enough, so it's much better to create custom ExpectedConditions according to the element attribute as described here然而,这种方法并不是很好,因为它不够通用,所以最好根据元素属性创建自定义ExpectedConditions ,如此所述

I have Written an extension method in C# Selenium, this will help to wait until a particular element / attribute is visible.我在 C# Selenium 中编写了一个扩展方法,这将有助于等到特定元素/属性可见。

Call This method like: WaitUntilElementIsVisible(driver, By.Id("DemoId"));像这样调用这个方法:WaitUntilElementIsVisible(driver, By.Id("DemoId")); // example // 例子

public static bool WaitUntilElementIsVisible(IWebDriver browser, By by)
{
            int attemptToFindElement = 0;
            bool elementFound = false;
            IWebElement elementIdentifier = null;
            do
            {
                attemptToFindElement++;
                try
                {
                    elementIdentifier = browser.FindWebElement(by);
                    elementFound = (elementIdentifier.Displayed && elementIdentifier.Enabled) ? true : false;
                }
                catch (Exception)
                {
                    elementFound = false;
                }

            }
            while (elementFound == false && attemptToFindElement < 100);

            return elementFound;
}

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

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