Selenium send_keys可以根据文本输入来更改元​​素

时间:2020-04-22 12:57:08

标签: python html 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> 我怎么能用硒插入文本

2 个答案:

答案 0 :(得分:0)

尝试以下方法:

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

答案 1 :(得分:0)

尝试以下代码:

driver.find_element_by_css_selector('#snippet-editor-field-description').send_keys('test123')