Selenium将_键发送到根据文本输入更改的span元素

2024-09-30 18:16:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用selenium更改WordPress中帖子的Yoast SEO元描述。然而,我不能让它工作

这是文本框的代码,其中没有任何文本:

        <div class="DraftEditor-editorContainer">
            <div aria-autocomplete="list" aria-describedby="placeholder-3oen9" aria-expanded="false" aria-labelledby="3" class="notranslate public-DraftEditor-content" contenteditable="true" role="combobox" spellcheck="false" id="snippet-editor-field-description" style="outline: none; user-select: text; white-space: pre-wrap; overflow-wrap: break-word;">
                <div data-contents="true">
                    <div class="" data-block="true" data-editor="3oen9" data-offset-key="9otfs-0-0">
                        <div data-offset-key="9otfs-0-0" class="public-DraftStyleDefault-block public-DraftStyleDefault-ltr">
                          <span data-offset-key="9otfs-0-0">
                            <br data-text="true">
                          </span>
                      </div>
                    </div>
                </div>
            </div>
        </div>

如果文本框中有文本,则文本框的代码将更改。 这是包含文本的框:

<div class="DraftEditor-editorContainer">
    <div aria-autocomplete="list" aria-describedby="placeholder-3oen9" aria-expanded="false" aria-labelledby="3" class="notranslate public-DraftEditor-content" contenteditable="true" role="combobox" spellcheck="false" id="snippet-editor-field-description" style="outline: none; user-select: text; white-space: pre-wrap; overflow-wrap: break-word;">
        <div data-contents="true">
            <div class="" data-block="true" data-editor="3oen9" data-offset-key="9otfs-0-0">
                <div data-offset-key="9otfs-0-0" class="public-DraftStyleDefault-block public-DraftStyleDefault-ltr">
                  <span data-offset-key="9otfs-0-0">
                    <span data-text="true">EXAMPLE TEXT</span>
                  </span>
                </div>
            </div>
        </div>
    </div>
</div>

如您所见<br data-text="true">变为<span data-text="true">EXAMPLE TEXT</span> 我怎样才能使用selenium插入文本


Tags: keytext文本divfalsetruedatapublic
2条回答

请尝试以下内容:

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='notranslate public-DraftEditor-content' and @role='combobox']"))).send_keys("Text Test")

注意:请将以下导入添加到您的解决方案中

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait

请尝试以下代码:

&13; 第13部分,;
driver.find_element_by_css_selector('#snippet-editor-field-description').send_keys('test123')
和#13;
和#13;

相关问题 更多 >