页面刷新后Selenium C#陈旧元素错误
我有一个问题,机器人给了一个错误的“过时的元素”,但它只是在第一个“军官”。起初我认为这与没有足够的时间刷新页面有关,因此技术上元素不存在,但在我添加了thread.sleep之后,它仍然抛出错误页面刷新后Selenium C#陈旧元素错误,c#,selenium-webdriver,C#,Selenium Webdriver,我有一个问题,机器人给了一个错误的“过时的元素”,但它只是在第一个“军官”。起初我认为这与没有足够的时间刷新页面有关,因此技术上元素不存在,但在我添加了thread.sleep之后,它仍然抛出错误 using System; using System.Threading; using OpenQA.Selenium; using OpenQA.Selenium.Chrome; using OpenQA.Selenium.Support.Extensions; using OpenQA.Selen
using System;
using System.Threading;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.Extensions;
using OpenQA.Selenium.Support.UI;
namespace bot_test
{
class Program
{
static void Main(string[] args)
{
// url used to create this -https://www.guru99.com/selenium-csharp-tutorial.html
IWebDriver driver = new ChromeDriver("C:\\");
// To change the date to find and replace datetovar to the date
//To change the date from find and replace darefromvar to the date
// Don't forget to switch the date back to the var format
string datefromvar = "12/05/2019";
string datetovar = "12/21/2019";
driver.Url = "https://odyssey.gwinnettcourts.com/Portal/Home/Dashboard/26";
driver.Manage().Window.Maximize();
// Officer 1- Argo, James .R
// Category Select
IWebElement typeElement = driver.FindElement(By.Id("cboHSSearchBy"));
var typeSelect = new SelectElement(typeElement);
typeSelect.SelectByValue("JudicialOfficer");
// JudicialOfficer ID
IWebElement officerId = driver.FindElement(By.Id("selHSJudicialOfficer"));
var officerIdSelect = new SelectElement(officerId);
officerIdSelect.SelectByValue("12942");
// Date From
IWebElement fromDate = driver.FindElement(By.Id("SearchCriteria_DateFrom"));
fromDate.SendKeys(datefromvar);
// Date To
IWebElement toDate = driver.FindElement(By.Id("SearchCriteria_DateTo"));
toDate.SendKeys(datetovar);
// Click Button
IWebElement buttonSubmitElement = driver.FindElement(By.Id("btnHSSubmit"));
buttonSubmitElement.Click();
driver.Navigate().Back();
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(15);
// Officer 2- Assistance, Judicial
Thread.Sleep(2000);
IWebElement typeElement2 = driver.FindElement(By.Id("cboHSSearchBy"));
typeSelect.SelectByValue("JudicialOfficer");
IWebElement officerId2 = driver.FindElement(By.Id("selHSJudicialOfficer"));
officerIdSelect.SelectByValue("19024"); //Assistance, Judicial
fromDate.SendKeys(datefromvar);
toDate.SendKeys(datetovar);
buttonSubmitElement.Click();
driver.Navigate().Back();
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(6);
// Officer 3- Ballar Christopher, A
Thread.Sleep(2000);
IWebElement typeElement3 = driver.FindElement(By.Id("cboHSSearchBy"));
typeSelect.SelectByValue("JudicialOfficer");
IWebElement officerId3 = driver.FindElement(By.Id("selHSJudicialOfficer"));
officerIdSelect.SelectByValue("12933"); //Ballar Christopher, A
fromDate.SendKeys(datefromvar);
toDate.SendKeys(datetovar);
buttonSubmitElement.Click();
driver.Navigate().Back();
}
}
}
我对此进行了更多的研究,发现问题在于,每次刷新新页面时,我没有为“typeElement”、“OfficerSelect”、“toDate”、“fromDate”和“ButtonsSubmitement”声明新的web元素,这导致我试图使用的元素变得过时 下面是修复错误的新代码 它可能不是最有效的,因为我不是C#或Selenium大师,但它很有效
// Officer 3- Ballar Christopher, A
Thread.Sleep(2000);
IWebElement typeElement3 = driver.FindElement(By.Id(elemSrchByStr));
var typeSelect3 = new SelectElement(typeElement3);
typeSelect3.SelectByValue(judOfficer);
IWebElement officerId3 = driver.FindElement(By.Id(officerSrchByStr));
var officerIdSelect3 = new SelectElement(officerId3);
officerIdSelect3.SelectByValue("12933"); //Ballar Christopher, A
IWebElement fromDate3 = driver.FindElement(By.Id(srchDateFrom));
fromDate3.SendKeys(dateFromVar);
IWebElement toDate3 = driver.FindElement(By.Id(srchDateTo));
toDate3.SendKeys(dateToVar);
IWebElement buttonSubmitElement3 = driver.FindElement(By.Id(button));
buttonSubmitElement3.Click();
driver.Navigate().Back();
您了解什么是StaleElementException以及它的原因吗?如果没有,你应该花些时间研究它。一旦你明白了这一点,修复它就会容易得多。我已经做了更多的研究,并添加了一个答案。出于某种原因,我认为这将是一个更复杂的解决方案。