Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用Selenium将文本插入文本字段?_Python_Selenium_Automation_E2e Testing_Geckodriver - Fatal编程技术网

Python 如何使用Selenium将文本插入文本字段?

Python 如何使用Selenium将文本插入文本字段?,python,selenium,automation,e2e-testing,geckodriver,Python,Selenium,Automation,E2e Testing,Geckodriver,我正在用Python和Selenium进行一个项目。 在图像中,您可以看到我尝试插入文本的输入字段的代码 <input data-v-382b0a6e="" id="expiration" name="expiration" type="text" required="required" data-test="expiration-input" autocomplete=

我正在用Python和Selenium进行一个项目。 在图像中,您可以看到我尝试插入文本的输入字段的代码

<input data-v-382b0a6e="" id="expiration" name="expiration" type="text" required="required" data-test="expiration-input" autocomplete="billing cc-exp" dir="auto" class="form-textbox form-textbox-text form-textbox-entered" style="" aria-invalid="true">
下图显示了错误

我还尝试使用Javascript执行器在这个输入字段中插入一个值,但它没有value属性,也不会使用这个属性

我不知道我还能在这里做什么。 有人提示我如何将文本插入此字段吗?

要向元素发送字符序列,而不是
元素的存在位置()
必须使
元素可点击()
,并且可以使用以下任一方法:

  • 使用
    CSS\u选择器

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#expiration[name='expiration'][data-test='expiration-input']"))).send_keys(expMonth)
    
  • 使用
    XPATH

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='expiration' and @name='expiration'][@data-test='expiration-input']"))).send_keys(expMonth)
    
  • 注意:您必须添加以下导入:

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

嗨,谢谢你的帮助。我试图使用你建议的解决方案,但我遇到了与以前相同的问题。我没有足够的帖子在聊天中写:/@kselight你没有提到你面临的任何具体问题/错误。你只是说没用。使用基于文本的相关HTML更新问题,并观察到错误。
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC