简体   繁体   English

python selenium无法清除输入字段

[英]python selenium can't clear input field

I try to select my input with selenium but when I use this it doesn't work:我尝试使用 selenium 选择我的输入,但是当我使用它时它不起作用:

driver = self.driver
password = driver.find_element_by_xpath("//input[@name='password']")
password.clear()
password.send_keys(password)
password.send_keys(Keys.RETURN)

# the sentence below doesn't work
password.send_keys(Keys.COMMAND, 'a')
password.send_keys(Keys.DELETE)

I am using Mac so Keys.CONTROL doesn't work, can anyone help me how to select the input or how to clear it?我使用的是 Mac,所以 Keys.CONTROL 不起作用,谁能帮助我如何选择输入或如何清除它?

Thanks谢谢

You have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies :您必须为element_to_be_clickable()引入WebDriverWait ,并且您可以使用以下任一定位器策略

  • Using CSS_SELECTOR :使用CSS_SELECTOR

     password = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='password']"))) password.click() password.clear() password.send_keys("Tijmen")
  • Using XPATH :使用XPATH

     password = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@name='password']"))) password.click() password.clear() password.send_keys("Tijmen")
  • Note : You have to add the following imports :注意:您必须添加以下导入:

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

Reference参考

You can find a couple of relevant discussions in:您可以在以下位置找到一些相关讨论:

Mac cannot use COMMAND you need Keys.BACKSPACE Try: Mac 无法使用COMMAND您需要Keys.BACKSPACE尝试:

driver = self.driver
password = driver.find_element_by_xpath("//input[@name='password']")
password.clear()
password.send_keys(password)
password.send_keys(Keys.RETURN)
#password = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@name='password']")))
#or
#time.sleep(1)
for i in range(len(password)):
    password.send_keys(Keys.BACKSPACE)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM