Selenium Web驱动器陈旧参考异常



我必须单击页面上的某个按钮。但是,当我检索具有特定类名称的所有元素时。当我尝试执行每个元素或单击时,所有检索到的元素都会引发过时的参考异常。我不能双击其中任何一个。它找到了正确的元素,但给所有这些元素提供了例外。评论的代码是我实际尝试选择并单击适当的按钮的地方。我附上了表格的图片。请注意,每次单击或执行按钮时都会更改页面。选择上传BOM按钮是您需要特别注意的。网站

 // Switch to correct frame
        IWebElement editorFrame = driver.FindElement(By.ClassName("frame-banner"));
        driver.SwitchTo().Frame(editorFrame);
        var action = new OpenQA.Selenium.Interactions.Actions(driver);
        // Select Project File 
        IList<IWebElement> projectFileButtonList= driver.FindElements(By.ClassName("data-cell"));
        foreach (var button in projectFileButtonList)
        {
            if (button.Text == "BOM_scrub")
            {
                // Found Project File now select it
                action.DoubleClick(button);
                action.Perform();
                break;
            }
        }
        // Select Upload BOM Button
        IList<IWebElement> uploadBomBtn = driver.FindElements(By.ClassName("se-custom-main-button"));
        foreach (var element in uploadBomBtn )
        {
            try
            {
                action.DoubleClick(element);
                action.Perform();
            }
            catch
            {
            }
            /*
            if (element.Text == "Upload BOM")
            {
                int i = 0;
                while (i == 0)
                {
                    try
                    {
                        action.DoubleClick(element);
                        action.Perform();
                        break;
                    }
                    catch
                    {
                    }
                }
            }
            */
        }

不要将driver.findElement(-s)与动态组件一起使用。

StaleElementReferenceException发生,因为您试图对元素执行一个动作,该动作已经从DOM脱离了。

您必须使用显式等待机制(WebDriverWait ExpectedConditions的组合),该机制会自动刷新元素的状态,并在满足指定条件时返回其有效表示。

最新更新