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
C# 如何在C中实现等待属性更改的Selenium条件#_C#_Selenium - Fatal编程技术网

C# 如何在C中实现等待属性更改的Selenium条件#

C# 如何在C中实现等待属性更改的Selenium条件#,c#,selenium,C#,Selenium,我想使用Selenium实现一个方法,该方法轮询HTML元素的一个属性的值,并等待它与给定的值(在本例中是之前的值)不同。下面的代码是我为此实现的方法 private static string waitForAttributeToNotBe(By elementCondition, string attribute, string originalValue) { Func<IWebDriver, bool> testCondition = (x) => !(x

我想使用Selenium实现一个方法,该方法轮询HTML元素的一个属性的值,并等待它与给定的值(在本例中是之前的值)不同。下面的代码是我为此实现的方法

 private static string waitForAttributeToNotBe(By elementCondition, string attribute, string originalValue)
 {
     Func<IWebDriver, bool> testCondition = (x) => !(x.FindElement(elementCondition).GetAttribute(attribute).Equals(originalValue));

     //Wait is implemented above, as Wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(20));

     Wait.Until(testCondition);

     return Driver.FindElement(elementCondition).GetAttribute(attribute);
 }
对定义Func的行求值时引发此异常

我的假设是,通过提供的条件显式定义FindElement可以避免元素未被缓存的问题

我能做些什么来避免这种情况吗?更明智的重新措辞

谢谢,,
庄信万丰试试这个。

在爪哇-

WebDriverWait wait = new WebDriverWait(driver, 30); 
wait.until(ExpectedCondition.attributeContains(WebElement element,
                                               java.lang.String attribute,
                                               java.lang.String value));
在C#-

您可以创建自己的自定义预期条件

请参阅此-

试试这个。

在爪哇-

WebDriverWait wait = new WebDriverWait(driver, 30); 
wait.until(ExpectedCondition.attributeContains(WebElement element,
                                               java.lang.String attribute,
                                               java.lang.String value));
在C#-

您可以创建自己的自定义预期条件

请参阅此-

下面是一个等待属性不同的示例:

string valueBefore=wait.Until(NotAttribute(By.cssSelector(…),“value”,”);
...
字符串valueAfter=wait.Until(NotAttribute(由.cssSelector(…),“value”,valueBefore));
公共静态函数NotAttribute(按定位器、字符串属性、字符串notValue){
返回(驱动程序)=>{
试一试{
var值=driver.FindElement(定位器).GetAttribute(属性);
返回值==notValue?空:值;
}
捕获(无接触元素异常){
返回null;
}
捕获(StaleElementReferenceException){
返回null;
}
};
}

下面是一个等待属性不同的示例:

string valueBefore=wait.Until(NotAttribute(By.cssSelector(…),“value”,”);
...
字符串valueAfter=wait.Until(NotAttribute(由.cssSelector(…),“value”,valueBefore));
公共静态函数NotAttribute(按定位器、字符串属性、字符串notValue){
返回(驱动程序)=>{
试一试{
var值=driver.FindElement(定位器).GetAttribute(属性);
返回值==notValue?空:值;
}
捕获(无接触元素异常){
返回null;
}
捕获(StaleElementReferenceException){
返回null;
}
};
}

这是Java。C#没有此预期条件。此预期条件在.NET选项中不存在(请参阅)。不过,看看源代码,它似乎与我的想法相似:
@Override public Boolean apply(WebDriver-driver){return getAttributeOrCssValue(findelelement(locator,driver),attribute).map(seen->seen.contains(value)).orElse(false);}
是的,这就是我实现的条件。请参见第一个代码段中Func testCondition的定义。此问题通常发生在DOM发生更改并使元素无法访问时。将该行放入try-and-catch(StaleElementReferenceException e)中,并将该try-catch放入循环2-3中,这样DOM就可以准备就绪。如果try块中未引发异常,则使用break退出循环。这是Java。C#没有此预期条件。此预期条件在.NET选项中不存在(请参阅)。不过,看看源代码,它似乎与我的想法相似:
@Override public Boolean apply(WebDriver-driver){return getAttributeOrCssValue(findelelement(locator,driver),attribute).map(seen->seen.contains(value)).orElse(false);}
是的,这就是我实现的条件。请参见第一个代码段中Func testCondition的定义。此问题通常发生在DOM发生更改并使元素无法访问时。将该行放入try-and-catch(StaleElementReferenceException e)中,并将该try-catch放入循环2-3中,这样DOM就可以准备就绪。如果try块中未引发异常,则使用break退出循环。我已更新了答案。请看一看,我已经更新了我的答案。请看一看,我还没有能够重现我的问题-这可能与网站速度慢和文本执行同步有关。不过,我会尝试你的解决方案,看看会发生什么。我还没有能够重现我的问题-这可能与网站速度慢和文本执行一致有关。不过,我会尝试你的解决方案,看看会发生什么。