簡體   English   中英

如何使用 Selenium 和 Python 在輸入中插入一些文本

[英]How to insert some text within the input using Selenium and Python

在網頁中,我有以下元素:

<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")

但它不起作用。

元素的快照:

在此處輸入圖像描述

要將字符序列發送到您需要為element_to_be_clickable()引入WebDriverWait的元素,您可以使用以下任一定位器策略

  • 使用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
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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM