简体   繁体   English

如何使用 Selenium Python 清除文本字段

[英]How to clear text field with Selenium Python

Here's my code and what I tried so far:这是我的代码以及到目前为止我尝试过的内容:

from selenium import webdriver
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.ui import WebDriverWait     
from selenium.webdriver.common.by import By     
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys

PATH = "driver\chromedriver.exe"
driver = webdriver.Chrome(executable_path=PATH)
url = 'https://www.mergermarket.com/homepage'
driver.get(url)

deals = driver.find_element_by_xpath('//*[@id="header"]/div/div[2]/nav/ul/li[4]/a')

urldeals = deals.get_attribute("href")
driver.get(urldeals)

custom1 = driver.find_element_by_xpath('//*[@id="searchCriteriaSummary"]/div[4]/div/div[2]/div/div[1]/div[2]/div/div/div[2]/div')
custom1.click()

custom2 = driver.find_element_by_id('react-select-12-option-0')
custom2.click()
time.sleep(1)

date = driver.find_element_by_xpath('//*[@id="date-picker_from"]')
date.click()
date.send_keys('01/11/2021') 
time.sleep(1)

date2 = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="date-picker_to"]')))
date2.click()
date2.clear()
date2.send_keys("15/11/2021")

The two last paragraph is where I struggle.最后两段是我挣扎的地方。

I have this:我有这个:

日期

When I enter 01/11/2021 in the input text at the left, the website automatically write the actual date on the right ( 26/11/2021 right now).当我在左侧输入文本中输入01/11/2021时,网站会自动在右侧写入实际日期(现在是26/11/2021 )。

I would like to clear the actual day and put my date but I cannot found how to do that.我想清除实际日期并输入我的日期,但我找不到如何做到这一点。 I did this code:我做了这段代码:

date2 = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="date-picker_to"]')))
date2.click()
date2.clear()
date2.send_keys("15/11/2021")

But it just write my date after the actual date:但它只是在实际日期之后写下我的日期:

日期2

I tried several methods but always th same results.我尝试了几种方法,但结果总是相同。 here the html code if it could help:如果有帮助,这里是 html 代码:

html

Instead of this而不是这个

date2 = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="date-picker_to"]')))
date2.click()
date2.clear()
date2.send_keys("15/11/2021")

You could trigger ctrl+A and delete like this您可以像这样触发ctrl+Adelete

date2 = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="date-picker_to"]')))
date2.click()
date2.send_keys(Keys.CONTROL + "a")
date2.send_keys(Keys.DELETE)
date2.send_keys("15/11/2021")

Imports:进口:

from selenium.webdriver.common.keys import Keys

Also, In the same way, you could trigger Backspace as well.同样,您也可以触发Backspace

date2 = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="date-picker_to"]')))
date2.click()
date2.sendKeys(Keys.BACK_SPACE)  
date2.send_keys("15/11/2021")

Trigger ctrl+a with:触发 ctrl+a :

from selenium.webdriver.common.keys import Keys

That'll delete everything first:)这将首先删除所有内容:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 python selenium无法清除输入字段 - python selenium can't clear input field 如何在 python 中使用类似的开关盒清除文本字段 - How to clear text field using alike switch case in python 如何使用Python Selenium将文本发送到框架中的输入字段中 - How to send text into a input field within a frame using Python Selenium 如何使用selenium python替换文本字段中的默认值? - How to replace default values in the text field using selenium python? 如何使用 Selenium 和 Python 将文本发送到密码字段 - How to send text to the Password field using Selenium and Python 如何使用python从硒的禁用字段中获取文本 - how to get text from disabled field in selenium using python 如何使用Selenium和Python将文本发送到电子邮件字段 - How to send text to the Email field using Selenium and Python 如何禁用自动完成以使用 selenium chrome python 将文本发送到用户名字段 - How to disable autocomplete to send text to username field with selenium chrome python 如何输入在Python Selenium中自动填充的文本字段 - How to input a text field which is autopopulating in Python Selenium 如何通过 Selenium Python 将文本发送到搜索字段 - How to send text to the search field through Selenium Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM