【问题标题】:the get title verifecation not identife by name on selenium获取标题验证不能在硒上按名称识别
【发布时间】:2020-05-28 01:50:54
【问题描述】:

我尝试验证标题是否正确,但它被卡住了

这是我的代码:

      string Expecttitle = "Consular Department in Jerusalem";
      string actualValue = element.FindElement(By.Name("PageTitle")).Text;
      Assert.AreEqual(actualValue, Expecttitle);

这是html

<h1 name="PageTitle" class="h2 reforma-medium xs-mb-15 lg-mb-20">Consular Department in Jerusalem</h1>

收到此错误 测试方法1 来源:UnitTest1.cs 第 19 行 持续时间:13.1 秒

消息: 测试方法 hw.UnitTest1.TestMethod1 抛出异常: OpenQA.Selenium.StaleElementReferenceException:过时的元素引用:元素未附加到页面文档 (会话信息:chrome=83.0.4103.61) 堆栈跟踪: RemoteWebDriver.UnpackAndThrowOnError(响应错误响应) RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary2 parameters) RemoteWebElement.Execute(String commandToExecute, Dictionary2参数) RemoteWebElement.FindElement(字符串机制,字符串值) RemoteWebElement.FindElementByName(字符串名称) c__DisplayClass18_0.b__0(ISearchContext 上下文) By.FindElement(ISearchContext 上下文) RemoteWebElement.FindElement(通过) UnitTest1.TestMethod1() 第 41 行

我要检查的文本

【问题讨论】:

  • 预期的标题值是多少?还是 FindElement 失败了?
  • 您正在从“元素”中查找元素 - 您正在查找的元素是什么? driver.FindElement(By.Name("PageTitle")).Text; 应该在 driver 是您的 webdriver 实例的情况下工作。
  • 使问题变得更好: 1. 添加您得到的实际错误。 2. 显示创建变量“元素”的代码。 3. 显示更多的 DOM (HTML)

标签: c# selenium selenium-webdriver


【解决方案1】:
  string actualValue = element.FindElement(By.Name("PageTitle")).Text;

这会搜索element 范围内的元素吗?它可能不包含&lt;h1&gt;

您可以通过打印 element 的 innerHtml 进行调试,如果它包含您要查找的名称,请参阅手册:

Console.WriteLine(element.GetAttribute("innerHTML"));

可能您想在整个页面中搜索By.Name(),例如:

  string actualValue = driver.FindElement(By.Name("PageTitle")).Text;

【讨论】:

  • 否,因为错误“stale element reference: element is not attach to the page document”表示您使用的 WebElement 引用在您尝试使用它时不在页面中。如果没有完整的代码和网页本身,我无法调试它。可能您使用“元素”。在您导航到另一个页面之后,或者该页面是动态的并且 Selenium 找到了它,但是当您尝试使用它时它被重绘或消失了。您可以将其包装在 try/catch 中,然后在 catch 块中再次找到它。研究陈旧元素。
猜你喜欢
  • 2020-01-17
  • 1970-01-01
  • 2012-07-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多