c# - Wordpress 5.2.2 和 selenium:无法找到正文帖子(不在 iframe 或 TinyMCE 中的富文本编辑器)
问题描述
我是硒和 C# 的新手。我正在尝试创建一个测试,以便我可以在我的 Wordpress 页面中创建一个基本帖子。我的测试用例失败了,因为它无法找到不在 iframe 或 TinyMCE 中的富文本编辑器。希望任何人都可以帮助我。谢谢。
我的测试用例:
[TestMethod]
public void Can_Create_A_Basic_Post()
{
LoginPage.GoTo();
LoginPage.LoginAs("admin").WithPassword("hatran").Login();
NewPostPage.GoTo();
NewPostPage.CreatePost("This is the post title")
.WithBody("Hi, This is the post body")
.Publish();
NewPostPage.GoToNewPost();
Assert.AreEqual(PostPage.Title, "This is the post title", "Title did not match new post.");
}
在我的自动化框架中导致错误的方法:
public void Publish()
{
Driver.Instance.FindElement(By.Id("post-title-0")).SendKeys(title);
Driver.Instance.FindElement(By.XPath("//div[@class='components-autocomplete']/p[@aria-label='Empty block; start writing or type forward slash to choose a block']"))
.SendKeys(body);
Driver.Instance.FindElement(By.XPath("//button[text() = 'Publish…']")).Click();
}
我尝试了以下代码来定位元素和发送键,但它没有工作
- 简单的代码
Driver.Instance.FindElement(By.XPath("//div[@id='wpbody-content']/div[@class='components-autocomplete']/p[@aria-label='空块;开始写或键入正斜杠以选择一个块']")).SendKeys(body);
或 2. 使用 IJavaScriptExecutor
IJavaScriptExecutor jscript = Driver.Instance as IJavaScriptExecutor;
var postBody = Driver.Instance.FindElement(By.XPath("//div[@class='components-autocomplete']/p[@aria-label='Empty block; start writing or type forward slash to choose a block']"));
IJavaScriptExecutor executor = (IJavaScriptExecutor)Driver.Instance;
executor.ExecuteScript("arguments[0].innerHTML = 'hello body post via javascript'", postBody);
预期结果:可以创建具有标题和正文的新帖子
实际结果:异常:
信息:
Test method WordpressTests.CreatePostTests.Can_Create_A_Basic_Post threw exception:
OpenQA.Selenium.NoSuchElementException: Unable to locate element: //div[@class='components-autocomplete']/p[@aria-label='Empty block; start writing or type forward slash to choose a block']
开始书写或输入 / 选择一个块
解决方案
推荐阅读
- gradle - 在 Gradle 多项目中,子项目的 SourceSets 是否有作用域?
- php - 我们如何获得动态下拉菜单以根据先前的下拉菜单显示特定选项,并能够随机化输出?
- java - 用vue和spring下载excel文件
- mysql - 尝试从链接服务器检索数据 - 在“等待初始通信数据包”时丢失与 MySQL 服务器的连接,系统错误:10060”
- python-3.x - 无法在标签上显示变量
- javascript - React - 如何构建一个简单的按钮应用程序?
- tfs - 如何从 TFS WorkItemTrackingHttpClient QueryByWiqlAsync 分页结果
- java - 找不到适合 jdbc:postgresql - 可执行的 java jar 的驱动程序
- java - EventSourceHandler 被错误的事件触发?
- javascript - 为什么 for..of 不能处理这个规范函数