I am trying to set input field of a Modal Form using Python's Selenium
Here's how the form's input looks like :
<input step="any" class="input" placeholder="0" value="">
I set the input value with this script:
script = "document.querySelector("input.input").value='1'"
driver.execute_script(script)
time.sleep(3)
My script does enter input value in the field but it remains there only for 3 Seconds and that too due time.sleep(3)
, otherwise it resets instantly to 0.
How can I set it to remain there and not to reset?
When I try normally on a HTML page, it does not resets. It does so only on that web page.
Kindly help
CodePudding user response:
The following code works fine.
# changed it from 'input.input' -> '.input'
script = "document.querySelector('.input').value='1'"
driver.execute_script(script)
time.sleep(3)
Also, use single inverted commas '
to catch the element class because you are using double inverted commas "
to declare the script for your script variable.
When trying to catch elements by their class name to execute queries:
script = "document.querySelector('.class_name').value='1'"
When trying to catch elements by their ID to execute queries:
script = "document.querySelector('#ID').value='1'"
Alternatively, you can use the send_keys
method too:
# entering the xpath of the text box you want to send your keys to
driver.find_element_by_xpath("//*[@id='body']/div[8]/div/div/div/div[2]/div[1]/div[2]/div[2]/div[3]/input").send_keys("1")
CodePudding user response:
wait=WebDriverWait(driver, 10)
url='https://zapper.fi/?protocol=sushiswap&contractAddress=0xe62ec2e799305e0d367b0cc3ee2cda135bf89816&modal=invest&target=/invest'
driver.get(url)
elem=wait.until(EC.element_to_be_clickable((By.CLASS_NAME,"input")))
elem.send_keys('1')
Send keys didn't change back after a few seconds.
Import:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
Outputs: