C# For循环中的Selenium Webdriver C下拉列表过时元素引用

C# For循环中的Selenium Webdriver C下拉列表过时元素引用,c#,selenium-webdriver,C#,Selenium Webdriver,当我运行For循环时,我只能迭代一次,而没有显示为过时的值。发生的情况是,我需要从第一个下拉列表中选择值来填充第二个下拉列表的选项 我试图以一种不应显示为过时的方式设置元素引用 //Order Type dropdown menu String OrderTypeDropDown = "//*[@id='OrderTypeId']"; IWebElement drpOrderType = driver.FindElement(By.XPath(OrderTypeD

当我运行For循环时,我只能迭代一次,而没有显示为过时的值。发生的情况是,我需要从第一个下拉列表中选择值来填充第二个下拉列表的选项

我试图以一种不应显示为过时的方式设置元素引用

//Order Type dropdown menu
        String OrderTypeDropDown = "//*[@id='OrderTypeId']";
        IWebElement drpOrderType = driver.FindElement(By.XPath(OrderTypeDropDown));            

        //Select the order type dropdown
        SelectElement select = new SelectElement(drpOrderType);

        //Make a list of all order type dropdown options 
        IList<IWebElement> elements = select.Options;        

        //Itterate through all the options in the order type dropdown
        foreach (IWebElement value in elements)
        {
            //Click the option
            value.Click();

            //Print out the option
            Console.WriteLine(value.Text);

            //Sleep
            Thread.Sleep(3000);

            //Change Type dropdown menu stale element reference setup
            String RequestChangeDropDown = "//*[@id='RequestedChange']";

            IWebElement drpChangeType = driver.FindElement(By.XPath(RequestChangeDropDown));

            //Select the Requested Change dropdown
            SelectElement drpSelect = new SelectElement(drpChangeType);
            driver.FindElement(By.XPath(RequestChangeDropDown)).Click();

            //Make a list of all Requested Change types
            IList<IWebElement> change = drpSelect.Options;

            foreach (IWebElement option in change)
            {
                Console.WriteLine(option.Text);                   
            }

        }
错误消息: OpenQA.Selenium.StaleElementReferenceException:stale元素引用:元素未附加到页面文档 会话信息:chrome=77.0.3865.90 堆栈跟踪: RemoteWebDriver.UnpackantRownerErrorResponse错误响应 RemoteWebDriver.ExecuteTrestring driverCommandToExecute,Dictionary`2参数 RemoteWebElement。单击
TSR_Forms.Mobile_Device_Request_Orders第907行

您必须等待元素可单击:

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(5));
            wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeClickable(By.XPath(RequestChangeDropDown)));
因此,您可以单击它。
问题是您试图单击的元素在DOM中还不能单击

首先看一下您的代码,一切都是FindElement,如果您想要一个列表,您将需要FindElement,这样所有匹配xpath的项都会被迭代。