从下拉列表中选择值时,陈旧元素引用错误

时间:2019-04-28 07:40:45

标签: c# selenium

我有一个网页,其中包含三个选择下拉列表。第三个下拉列表中的值将根据前两个下拉列表中的值选择进行刷新。当我尝试在第三个下拉菜单中设置值时,出现错误:陈旧元素引用:元素未附加到页面文档。在FindElements中,我要求驱动程序等待直到从下拉列表中返回的元素计数> 1,但仍然出现错误。

当我调试(恰好在第三个下拉列表中选择值之前)测试并运行时,它运行良好。关于可能是什么问题的任何建议

我在这里尝试了解决方案:Selenium c# Webdriver: Wait Until Element is Present,并在SO上四处寻找线程,但是其中大多数是指使用硒中已弃用的ExpectedConditions

调用SelectElementFromDropDown的方法:

    public void FillInspectionForm(InspectionFormData inspectionFormData, bool fillPartial = false)
    {
        SeleniumSetMethods.SelectElementFromDropDown("CssSelector=select#inspectionType", "CssSelector=select#inspectionType option", inspectionFormData.InspectionType);
        SeleniumSetMethods.SelectElementFromDropDown("CssSelector=select#inspectionEquipments_0_equipmentCategory", "CssSelector=select#inspectionEquipments_0_equipmentCategory option", inspectionFormData.MachineType);
        SeleniumSetMethods.SelectElementFromDropDown("CssSelector=select#formInstance_formDefinition_formCode", "CssSelector=select#formInstance_formDefinition_formCode option", inspectionFormData.FormName);
    }

从下拉列表中选择值的方法:

            public static void SelectElementFromDropDown(string elementLocator, string elementLocatorOptions, string valueToBeSelected, int elementWaitTimeOut = 60)
    {
            SelectElement selectElement = new SelectElement(BaseMethods.GetElement(elementLocator));
            WebDriverWait wait = new WebDriverWait(DriverContext.driver, TimeSpan.FromSeconds(elementWaitTimeOut));
            var webElements = BaseMethods.GetElements(elementLocatorOptions);
            selectElement.SelectByText(valueToBeSelected);
    }

使用定位符查找元素的GetElement和GetELements:

    public static IWebElement GetElement(string elementLocator, int elementWaitTimeOut = 60)
    {
        IWebElement element = null;
        var elementData = elementLocator.Split(new[] { '=' }, 2);
        try
        {
            switch (elementData[0].ToString())
            {
                case "CssSelector":
                    element = FindElement(DriverContext.driver, By.CssSelector(elementData[1].ToString()), elementWaitTimeOut);
                    break;
            }
        }
        return element;
    }

    public static IWebElement FindElement(IWebDriver driver, By by, int timeoutInSeconds)
    {
        if (timeoutInSeconds > 0)
        {
                var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutInSeconds));
                return wait.Until(drv => drv.FindElement(by));
        }       
        return driver.FindElement(by);
    }

    public static ReadOnlyCollection<IWebElement> GetElements(string elementLocator, int elementWaitTimeOut = 60)
    {
        ReadOnlyCollection<IWebElement> elements = null;
        var elementData = elementLocator.Split(new[] { '=' }, 2);
            switch (elementData[0].ToString())
            {
                case "CssSelector":
                    elements = FindElements(DriverContext.driver, By.CssSelector(elementData[1].ToString()), elementWaitTimeOut);
                    break;
            }
        return elements;
    }

    public static ReadOnlyCollection<IWebElement> FindElements(IWebDriver driver, By by, int timeoutInSeconds)
    {
        if (timeoutInSeconds > 0)
        {
            var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutInSeconds));
            return wait.Until(drv => (drv.FindElements(by).Count > 1) ? drv.FindElements(by) : null);
        }
        return driver.FindElements(by);
    }

实际结果:陈旧元素引用错误 预期结果:没有过时的元素引用错误

0 个答案:

没有答案