I'm working on an ASP.NET MVC college project and some part of the project requires to automate the process of submitting a problem on codeforces.com the problem is that when selenium does not wait until the element is presented so I get an error :
OpenQA.Selenium.StaleElementReferenceException: 'stale element reference: element is not attached to the page document
and while debugging everything is working fine without any error. I have tried a lot of things to solve it but in the end all methods end with that error or something like that.
Some of the methods I used: * wait until * use dummy action to wait like Maximize and Minimize * System Sleep
And here is my code
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;
using OpenQA.Selenium.Support;
using NUnit.Framework;
namespace AutoCodeforces
{
[TestClass]
public class Automate
{
IWebDriver chromeDriver = new ChromeDriver();
[TestMethod]
public void OpenCodeforces()
{
string handle = "A4AJudge";
string password = "A4A123456789";
string ProblemID = "33A";
string LangValue = "54";
string code = @"#include <iostream> #include <algorithm> #include <string> #include <cstdio> #include <vector> using namespace std; int a[1000], i, n, m, j, l, k, ans; int main() { scanf(" + @"% d % d % d" + @", &n, &m, &k); for (i = 0; i < m; i++) a[i] = 1000001; for (i = 0; i < n; i++) { scanf(" + " % d % d" + ", &j, &l); j--; a[j] = min(a[j], l); } for (i = 0; i < m; i++) ans += a[i]; ans = min(ans, k); cout << ans << endl; return 0; }";
chromeDriver.Navigate().GoToUrl("http://codeforces.com/enter");
chromeDriver.Manage().Window.Maximize();
chromeDriver.FindElement(By.Id("handleOrEmail")).SendKeys(handle);
chromeDriver.FindElement(By.Id("password")).SendKeys(password);
chromeDriver.FindElement(By.ClassName("submit")).Click();
//WebDriverWait wait = new WebDriverWait(chromeDriver, TimeSpan.FromMinutes(1));
//IWebElement element = wait.Until(driver => driver.FindElement(By.XPath("//a[@href='/problemset']")));
chromeDriver.FindElement(By.XPath("//a[@href='/problemset']")).Click();
//element = wait.Until(driver => driver.FindElement(By.XPath("//a[@href='/problemset/submit']")));
chromeDriver.FindElement(By.XPath("//a[@href='/problemset/submit']")).Click();
//element = wait.Until(driver => driver.FindElement(By.XPath("//input[@name='submittedProblemCode']")));
chromeDriver.FindElement(By.XPath("//input[@name='submittedProblemCode']")).SendKeys(ProblemID);
IWebElement selecElement = chromeDriver.FindElement(By.Name("programTypeId"));
SelectElement language = new SelectElement(selecElement);
language.SelectByValue(LangValue);
chromeDriver.FindElement(By.Id("sourceCodeTextarea")).SendKeys(code);
chromeDriver.FindElement(By.ClassName("submit")).Click();
//chromeDriver.Close();
//chromeDriver.Quit();
}
}
}
Any help, please?