0

I have an input field on a webpage that I want to access and set the value. To do so, I have the following:

element = browser.find_element_by_id("input")
driver.execute_script('arguments[0].scrollIntoView()', element)
browser.wait_until_element_is_visible(element)
element.clear()
driver.execute_script(f'arguments[0].value = "{input_value}"', element)

The above works but sometimes when setting the input field's value it doesn't actually set it on the webpage, so the input field will be empty. But when I output: print(element.get_attribute('value')) , it is returning exactly what I wanted the input field to have (so following the example, input_value).

I want to change the input field on the UI by setting the input's value attribute and prefer not to use sendKeys() as these input strings are very long. So does anyone know how to make the input field value show up on the webpage?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
chrisans
  • 37
  • 4

1 Answers1

0

To access and set the value you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

driver.execute_script('arguments[0].scrollIntoView()', WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.ID, "input"))))
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, "input")))
element.click()
element.clear()
element.send_keys(input_value)

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
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352