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

How to clear text field with Selenium Python

这是我的代码和我到目前为止尝试过的代码:

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

最后两段是我挣扎的地方。

我有这个:

当我在左边的输入文本中输入01/11/2021时,网站会自动在右边写上实际日期(26/11/2021现在)。

我想清除实际日期并输入我的日期,但我找不到该怎么做。我做了这个代码:

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

但它只是在实际日期之后写我的日期:

我尝试了几种方法,但结果总是一样。这里是 html 代码,如果有帮助的话:

而不是这个

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

您可以像这样触发 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")

进口:

from selenium.webdriver.common.keys import Keys

同样,你也可以触发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")

触发 ctrl+a 使用:

from selenium.webdriver.common.keys import Keys

这会先删除所有内容:)