【发布时间】:2020-04-10 04:53:31
【问题描述】:
我正在处理一个 ASP.NET MVC 大学项目,该项目的某些部分需要自动化在 codeforces.com 上提交问题的过程 问题是当 selenium 不等到元素出现时,我得到一个错误:
OpenQA.Selenium.StaleElementReferenceException: 'stale element reference: element is not attached to the page document
在调试时一切正常,没有任何错误。 我已经尝试了很多方法来解决它,但最终所有方法都以该错误或类似的东西结束。
我使用的一些方法: * 等到 * 使用虚拟动作等待最大化和最小化 * 系统休眠
这是我的代码
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();
}
}
}
有什么帮助吗?
【问题讨论】:
-
这能回答你的问题吗? stackoverflow.com/a/12967602/11865571
标签: c# selenium automation selenium-chromedriver