如何在python中使用Selenium Webdriver将内容设置为mui-rte? [英] How to set content into mui-rte with selenium webdriver in python?

查看:112
本文介绍了如何在python中使用Selenium Webdriver将内容设置为mui-rte?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用mui-rte RTF编辑器( https://github.com/niuware/mui -rte ).写硒Webdriver集成测试时,我不知道如何在rte输入区域中输入文本.

I am using mui-rte rich text editor (https://github.com/niuware/mui-rte) in a react project. I am not able to figure out how to input text to the rte input area when writing a selenium webdriver integration test.

据我正确理解,mui-rte是draftjs的Materia-ui包装器.反应代码很简单:

As I understand correctly, mui-rte is a materia-ui wrapper of draftjs. The react code is simply:

<MUIRichTextEditor
        onChange={onChange}
        value={initial}
        {...rest}
/ >

这将生成以下html元素:

This generates the following html elements:

<div id="mui-rte-container" class="MUIRichTextEditor-container-73">
<div id="mui-rte-toolbar" class="MUIRichTextEditor-toolbar-84">
    ...
</div>
<div id="mui-rte-editor" class="MUIRichTextEditor-editor-75">
    <div id="mui-rte-editor-container" class="MUIRichTextEditor-hidePlaceholder-79 MUIRichTextEditor-editorContainer-76">
        <div class="DraftEditor-root">
            <div class="DraftEditor-editorContainer">
                <div aria-describedby="placeholder-9mnek" class="notranslate public-DraftEditor-content" role="textbox" spellcheck="false" style="outline:none;user-select:text;-webkit-user-select:text;white-space:pre-wrap;word-wrap:break-word" contenteditable="true">
                    <div data-contents="true"><div class="" data-block="true" data-editor="7kjuh" data-offset-key="8a2rc-0-0">
                        <div data-offset-key="8a2rc-0-0" class="public-DraftStyleDefault-block public-DraftStyleDefault-ltr">
                            <span data-offset-key="8a2rc-0-0">
                                <br data-text="true">
                            </span>
                        </div>
                    </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

我可以轻松找到任何元素,但是例如,当我尝试这样做时:

I can easily find any of the element but when I try this for example:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0
from selenium.webdriver.common.by import By

rte_editor = WebDriverWait(self.driver, 2).until(
        EC.presence_of_element_located((By.ID, id))
    )
rte_input = bio_rte.find_element_by_xpath("//div[@role='textbox']")
rte_input.send_keys("Hello")

我得到:

E       selenium.common.exceptions.ElementNotInteractableException: Message: Element <div class="notranslate public-DraftEditor-content"> is not reachable by keyboard

包含我尝试过的所有元素.

With all elements that I have tried.

在python中使用selenium-webdriver将文本输入到draft.js rte的正确方法是什么?我对selenium + webdriver还是很陌生,无论是python,JavaScript还是其他类型的selenium-webdriver API,都将不胜感激.

What is the correct way to input text into draft.js rte with selenium-webdriver in python? I am quite new to selenium+webdriver and any help will be appreciated, be it in python, JavaScript or other flavor of selenium-webdriver API.

我在这里有一个示例项目: https://github.com/vessper/formik-mui-rte-example

I have a sample project here: https://github.com/vessper/formik-mui-rte-example

包括来自错误的堆栈跟踪:

Including the stack trace from the error:

self = <test.TestBase testMethod=test_input_text_in_rte>

    def test_input_text_in_rte(self):
        rte_input = WebDriverWait(self.driver, 20).until(
            EC.element_to_be_clickable(
>               (By.XPATH, '//div[@class="notranslate public-DraftEditor-content" and @role="textbox"]'))
        )

test.py:25: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <selenium.webdriver.support.wait.WebDriverWait (session="38c21bf5-27ea-499d-9831-e8755a10f57a")>
method = <selenium.webdriver.support.expected_conditions.element_to_be_clickable object at 0x7f7115fe7198>, message = ''

    def until(self, method, message=''):
        """Calls the method provided with the driver as an argument until the \
        return value is not False."""
        screen = None
        stacktrace = None

        end_time = time.time() + self._timeout
        while True:
            try:
                value = method(self._driver)
                if value:
                    return value
            except self._ignored_exceptions as exc:
                screen = getattr(exc, 'screen', None)
                stacktrace = getattr(exc, 'stacktrace', None)
            time.sleep(self._poll)
            if time.time() > end_time:
                break
>       raise TimeoutException(message, screen, stacktrace)
E       selenium.common.exceptions.TimeoutException: Message:

../../../.virtualenvs/ml2/lib/python3.7/site-packages/selenium/webdriver/support/wait.py:80: TimeoutException
================================================================= 1 failed in 24.70s ==================================================================

推荐答案

在我的情况下,它是具有 contenteditable div draft.js 富文本编辑器. >

In my case it was a draft.js rich text editor with a contenteditable div

async sendKeysToContentEditableDiv(element, text) {
    const script = `var txtarea = arguments[0],txt = arguments[1];
    txtarea.dispatchEvent(new Event("focus"));
    var txtEvt = document.createEvent("TextEvent");
    txtEvt.initTextEvent("textInput", true, true, null, txt);
    txtarea.dispatchEvent(txtEvt);     
    txtarea.dispatchEvent(new Event("blur"));`;
    await this.driver.executeScript(script, element, text);
}

这篇关于如何在python中使用Selenium Webdriver将内容设置为mui-rte?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆