首页 > 解决方案 > 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();
}

我尝试了以下代码来定位元素和发送键,但它没有工作

  1. 简单的代码

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']

新帖页面源代码

富文本编辑器源码的另一张图

我的测试用例

将文本发送到块编辑器的代码

开始书写或输入 / 选择一个块

标签: c#selenium-webdriver

解决方案


推荐阅读