0

Any ideas on how to get the field to clear the pre-filled text prior to setting new text - I have attempted the previously recommended solutions and this is what I currently have - this merely just appends to the current pre-filled text:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait  
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
#Install Chrome Extension
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
#implicit wait
driver.implicitly_wait(0.5)
#maximize browser
driver.maximize_window()
#launch URL
driver.get("https://www.futbin.com/fut-card-creator/22")

#Set Vars
rating = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, "rating_change")))
rating.click()
rating.send_keys(Keys.CONTROL + "a")
rating.send_keys(Keys.BACK_SPACE)
rating.send_keys("90")

driver.implicitly_wait(0.5)

position = driver.find_element(By.ID, "position_change")
position.click()
position.send_keys(Keys.CONTROL + "a")
position.send_keys(Keys.BACK_SPACE)
position.send_keys("ST")
  • I woke up this morning and it is suddenly working... I am not sure why it wasn't working yesterday but it is functioning fine this morning – Devin Smith Jul 25 '22 at 07:53

2 Answers2

1

This is how you clear an input field in selenium (based on your existing code):

[...]
rating = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, "rating_change")))
rating.click()
rating.clear() ## field will be cleared and you can input your value
[...]
Barry the Platipus
  • 9,594
  • 2
  • 6
  • 30
  • I can confirm this does work aswell, when I tested this last night it wasn't functioning so went the CTRL A DEL route but yes this does work and is shorter! – Devin Smith Jul 25 '22 at 07:57
0

You can set the desired value within the desired element using setAttribute() method as follows:

  • Code block:

    driver.execute("get", {'url': 'https://www.futbin.com/fut-card-creator/22'})
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[aria-label='dismiss cookie message']"))).click()
    driver.execute_script("arguments[0].setAttribute('value','96')", WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#rating_change"))))
    
  • Browser snapshot:

96

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Nopes :) correct place. OP asked _"how to get the field to clear the pre-filled text"_, user input is accomodated within `value` attribute, so you can set it directly. – undetected Selenium Jul 25 '22 at 04:57
  • This does indeed replace the pre-filled out value, however, does not trigger the change on the card face on the left - need to trigger the send_keys in order to get it to trigger the card change – Devin Smith Jul 25 '22 at 08:03
  • Never mind, you seem to get a working [solution](https://stackoverflow.com/questions/73102327/field-not-clearing-selenium-python/73102391#comment129115622_73102391) ;) – undetected Selenium Jul 25 '22 at 08:10