[英]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:但它只是在实际日期之后写下我的日期:
I tried several methods but always th same results.我尝试了几种方法,但结果总是相同。 here the html code if it could help:如果有帮助,这里是 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+A
并delete
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.