C#:等待元素包含 Selenium 中的特定属性
C#: Wait for element to contain specific attribute in Selenium
我正在等待元素属性 aria-sort
等于“descending
”。
目前正在使用 C# Selenium 网络驱动程序。我该如何进行?我在 Visual Studio.
中没有看到 AttributeContains
boolean status = new WebDriverWait(driver, 20).until(ExpectedConditions.attributeContains(By.xpath("//div[@class='model-holder']/span[contains(.,'200K')]"), "class", "model-ready"));
除了您在问题中提到的 post 中介绍的方式外,您还可以执行以下操作:
假设元素可以通过以下 XPath //tag[@class='theClass']
唯一定位
因此,当此元素的 aria-sort
属性等于 descending
时,则可以通过此 XPath 定位此元素://tag[@class='theClass' and(@aria-sort='descending')]
所以你可以简单地使用常规 ElementExists
ExpectedConditions
如下:
boolean status = new WebDriverWait(driver, 20).until(ExpectedConditions.ElementExists(By.xpath("//tag[@class='theClass' and(@aria-sort='descending')]"));
但是这种方法不是很好,因为它不够通用,所以根据
描述的元素属性创建自定义 ExpectedConditions
会更好
我已经用 C# Selenium 编写了一个扩展方法,这将有助于等待特定元素/属性可见。
像这样调用此方法:WaitUntilElementIsVisible(driver, By.Id("DemoId")); // 示例
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;
}
我正在等待元素属性 aria-sort
等于“descending
”。
目前正在使用 C# Selenium 网络驱动程序。我该如何进行?我在 Visual Studio.
中没有看到 AttributeContainsboolean status = new WebDriverWait(driver, 20).until(ExpectedConditions.attributeContains(By.xpath("//div[@class='model-holder']/span[contains(.,'200K')]"), "class", "model-ready"));
除了您在问题中提到的 post 中介绍的方式外,您还可以执行以下操作:
假设元素可以通过以下 XPath //tag[@class='theClass']
唯一定位
因此,当此元素的 aria-sort
属性等于 descending
时,则可以通过此 XPath 定位此元素://tag[@class='theClass' and(@aria-sort='descending')]
所以你可以简单地使用常规 ElementExists
ExpectedConditions
如下:
boolean status = new WebDriverWait(driver, 20).until(ExpectedConditions.ElementExists(By.xpath("//tag[@class='theClass' and(@aria-sort='descending')]"));
但是这种方法不是很好,因为它不够通用,所以根据
ExpectedConditions
会更好
我已经用 C# Selenium 编写了一个扩展方法,这将有助于等待特定元素/属性可见。
像这样调用此方法:WaitUntilElementIsVisible(driver, By.Id("DemoId")); // 示例
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;
}