【问题标题】:How to insert some text within the input using Selenium and Python如何使用 Selenium 和 Python 在输入中插入一些文本
【发布时间】:2022-02-10 07:16:28
【问题描述】:

在网页中,我有以下元素:

<div id="learning_form" class="learning_form">
                <table cellspacing="0" cellpadding="0">
                    <tbody><tr><td><input type="text" id="answer" autocapitalize="off" autocorrect="off" autocomplete="off" placeholder="Odpowiedź" style="width: 360px;"></td></tr>
                </tbody></table>
                <div id="check"><h4 style="text-align: center;">Sprawdź</h4></div>
                <div id="special_characters"><div>&nbsp;</div><div>&nbsp;</div></div>
            </div>

如何在 python 中使用 selenium 在此处插入文本? 我尝试使用:

driver.find_element(By.ID,"learning_form").send_keys("some text")

但它不起作用。

元素的快照:

【问题讨论】:

  • 什么是“Sprawdz”?您确定元素的 ID 正是“learning_form”吗?

标签: python selenium xpath css-selectors webdriverwait


【解决方案1】:

要将字符序列发送到您需要为element_to_be_clickable() 诱导WebDriverWait 的元素,您可以使用以下任一Locator Strategies

  • 使用CSS_SELECTOR

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.learning_form#learning_form input#answer[placeholder='Odpowiedź']"))).send_keys("OłJeż")
    
  • 使用XPATH

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='learning_form' and @id='learning_form']//input[@id='answer' and @placeholder='Odpowiedź']"))).send_keys("OłJeż")
    
  • 注意:您必须添加以下导入:

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

【讨论】:

    【解决方案2】:
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#answer"))).send_keys("value")
    

    您似乎需要使用 id 答案向输入发送值。

    <input type="text" id="answer" autocapitalize="off" autocorrect="off" autocomplete="off" placeholder="Odpowiedź" style="width: 360px;">
    

    进口:

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

    【讨论】:

      猜你喜欢
      • 2013-09-04
      • 1970-01-01
      • 1970-01-01
      • 2021-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-10
      相关资源
      最近更新 更多