Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/314.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

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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.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# 在特定IWebElement上使用webdriverwait_C#_Selenium_Webdriver_Selenium Webdriver - Fatal编程技术网

C# 在特定IWebElement上使用webdriverwait

C# 在特定IWebElement上使用webdriverwait,c#,selenium,webdriver,selenium-webdriver,C#,Selenium,Webdriver,Selenium Webdriver,我正在将selenium webdriverwait方法应用于特定的IWebElement,以获取此IWebElement的一些可用子元素。这是我的密码 WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10)); IList<IWebElement> elementsA = wait.Until<IList<IWebElement>>((d) => { re

我正在将selenium webdriverwait方法应用于特定的IWebElement,以获取此IWebElement的一些可用子元素。这是我的密码

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
IList<IWebElement> elementsA = wait.Until<IList<IWebElement>>((d) =>
{
     return driver.FindElements(By.XPath("//table[@class='boxVerde']"));
});

foreach (IWebElement iwe in elementsA)
{
      wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
      IList<IWebElement> elementsB = wait.Until<IList<IWebElement>>((d) =>
      {
            return iwe.FindElements(By.XPath("tr/td/div/a"));
            //trying to fetch the anchor tags from the element
       });
}
WebDriverWait wait=new-WebDriverWait(驱动程序,TimeSpan.FromSeconds(10));
IList elementsA=等待直到((d)=>
{
返回driver.FindElements(By.XPath(//table[@class='boxVerde']);
});
foreach(IWebElement-iwe-in-elementsA)
{
wait=newwebdriverwait(驱动程序,TimeSpan.FromSeconds(10));
IList elementsB=等待直到((d)=>
{
返回iwe.FindElements(By.XPath(“tr/td/div/a”);
//正在尝试从元素获取锚定标记
});
}

它不断给我一个错误,说“元素不再连接到DOM”……我认为webdriver wait根本不起作用。伙计们,我做错什么了吗?提前非常感谢

试试这个。它等待元素出现在页面中

static void ForElementsArePresent(IWebDriver wd, double waitForSeconds, By by)
        {
            try
            {
                WebDriverWait wait = new WebDriverWait(wd, TimeSpan.FromSeconds(waitForSeconds));
                wait.Until(x => (x.FindElements(by).Count > 0));
            }
            catch (WebDriverTimeoutException)
            {
                Console.WriteLine("Waiting for element " + by + " to disappear timed out after " + waitForSeconds + " seconds.");
                throw;
            }                       
        }

听起来你好像是在忍受一种陈腐的元素

Selenium找到的每个WebElement实际上都是对DOM中某个元素的引用

有时,站点上使用的JavaScript会破坏并重新创建元素,当这种情况发生时,您现有的引用将变得陈旧,您将看到一个StaleElementReferenceException(至少在Java land中是这样称呼的,您应该看到非常类似的情况)

因此,如果您曾经看到StaleElementReferenceException或消息指出“元素不再附加到DOM”,这意味着对DOM中您曾经拥有的元素的引用不再有效,因为您拥有的引用的元素已被销毁

这在视觉上并不总是显而易见的,因为原始元素可能已被销毁,然后重新创建了一个相同的元素,但是它是一个新元素,因此需要创建一个新引用来与之交互

创建新引用的方法是再次查找元素

您的问题并不完全清楚,但我假设您在找到表后遍历锚元素列表时遇到了问题。如果是这种情况,则表明该表已在找到表元素的点之间被销毁并重新创建,然后开始遍历表中的锚元素


如果是这种情况,您将需要再次查找表,一个有用的技巧是等待表过时后再尝试再次查找。通过这种方式,您可以合理地确信页面上任何正在破坏表然后重新创建表的JavaScript都已经完成了处理。这家公司没有那家公司成熟。我建议看一看一些Java版本,并将它们移植到.NET(应该很简单,结构非常相似),特别值得关注的是等待元素过时的Java版本。

您必须定义Webdriver等待忽略异常,直到轮询时间结束。下面的java方法轮询元素util 60秒并返回它是否已加载,否则将引发超时异常。请根据需要将其转换为您的语言。(对不起,我是javaist)

或者通过By.linkText(“锚定标记文本”)定位元素


我相信该页面的某些表格已被删除。查看页面上的事件将非常有用。您应该使用
//table[@class='boxVerde']/tr/td/div/a
xpath,而不是在元素内部搜索。

什么版本的Selenium?什么浏览器和该浏览器的版本?是否尝试使用与XPath查询相同的CSS选择器?你需要等待吗?你好,Arran,我正在使用chromedriver_win_26.0.1383.0和selenium版本2…还没有尝试css选择器。老实说,我不认为我需要等待,因为在我打电话之前,我不会在中更改dom,但因为它给了我staleElementReferenceException“元素不再连接到dom”,我认为我不知何故取消了等待…你有什么Chrome版本?硒版本2点什么?最新的版本是v2.31,你在使用它吗?Chrome v 26.0.1410.43和selenium v2.31是的。如果谁投了反对票,请解释为什么考虑上述所有内容都是正确的……如果如你所建议的,表格已从页面中删除,那么使用XPath将有何帮助?
public WebElement fluentWait(final By locator, WebDriver driver) {
    Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
    .withTimeout(60, TimeUnit.SECONDS)
    .pollingEvery(1, TimeUnit.SECONDS)
    .ignoring(StaleElementReferenceException.class)
    .ignoring(NoSuchElementException.class);
    WebElement foo = wait.until(
    new Function<WebDriver, WebElement>() {
    public WebElement apply(WebDriver driver) {
    return driver.findElement(locator);
    }
         }  );
    return foo;}
       iwe.FindElements(By.XPath(" //a[text()='anchor tag text']"));
       iwe.FindElements(By.linkText("anchor tag text"));