【问题标题】:Selenium C# does not waitSelenium C# 不等待
【发布时间】: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();
        }       
    }
}

有什么帮助吗?

【问题讨论】:

标签: c# selenium automation selenium-chromedriver


【解决方案1】:

我认为您的等待在这里不够“明确”——您需要在 ExpectedConditions 类上调用 wait.Until,而不仅仅是在 driver.FindElement() 返回一个元素时。

ExpectedConditions for C# 现在特别包含在外部命名空间中——标准的 Selenium 绑定已被弃用。您需要DotNetSeleniumExtras.WaitHelpers NuGet 包才能使其工作。我将提供一个不需要额外依赖的替代解决方案。

使用DotNetSeleniumExtras.WaitHelpersExpectedConditions

WebDriverWait wait = new WebDriverWait(chromeDriver, TimeSpan.FromMinutes(1));

IWebElement element = wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeClickable(chromeDriver.FindElement(By.XPath("//a[@href='/problemset']"))));

不使用任何外部依赖,调用IWebElement.IsDisplayed()属性:

WebDriverWait wait = new WebDriverWait(chromeDriver, TimeSpan.FromMinutes(1));

var isDisplayed = wait.Until(chromeDriver => chromeDriver.FindElement(By.XPath("//a[@href='/problemset']")).Displayed);

chromeDriver.FindElement(By.XPath("//a[@href='/problemset']")).Click();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-12
    • 1970-01-01
    • 1970-01-01
    • 2022-01-07
    • 1970-01-01
    • 2017-06-09
    • 2018-11-30
    相关资源
    最近更新 更多