It looks like the input
's oninput
method makes it reset its value
. Changing/overriding it did it for me:
driver.execute_script('''function getElementByXpath(path) {
return document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
}
cnoinput = getElementByXpath("/html/body/div/div/div/div[1]/div/div[2]/div[1]/div/div/div/div/div/div[2]/div[1]/input");
cnoinput.oninput = function(){this.value = "0000 0000 0000 0000"};
cnoinput.removeAttribute('value');
''')
cnoinput
is the input element.
Changing what would happen when something's typed into it, by changing its oninput
function, that changes the input's value to "0000 0000 0000 0000" will prevent the default behaviour of the element and will only keep changing its value to "0000 0000 0000 0000".
Replace that function with what you'd like to do (paste the copied value from Excel).
I got the getElementByXpath(path)
function from here.
This is the full code:
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
import pyautogui
#Imports
driver = webdriver.Chrome(executable_path=r"chromedriver.exe")
driver.get("https://ibis-dev.droicelabs.us/login/practice")
email = ''
while not email:
try:
email = driver.find_element(By.NAME, 'email') #Get the Email input
except:
continue
#Wait till the input is loaded, but I think this method will be faster than WebDriverWait.
#https://stackoverflow.com/a/53023604/16136190
email.click()
pyautogui.write("krupal.practice@getnada.com") #"write" or type the Email
pwd = ''
while not pwd:
try:
pwd = driver.find_element(By.NAME, 'password')
except:
continue
pwd.click()
pyautogui.write("Test@2020")
login = ''
while not login:
try:
login = driver.find_element(By.XPATH, "//input[@value='SIGN IN']")
except:
continue
login.click()
time.sleep(2) #Assuming the time to log in is less than 2 seconds. If not, increase it.
driver.get("https://ibis-dev.droicelabs.us/practice/orders/61d7c50335afc005e70aac00/payment/?section=health_insurance")
#Because the login details are already stored, just load the page.
cardno = ''
while not cardno:
try:
cardno = driver.find_element(By.XPATH, "/html/body/div/div/div/div[1]/div/div[2]/div[1]/div/div/div/div/div/div[2]/div[1]/input")
except:
continue
cardno.click()
driver.execute_script('''function getElementByXpath(path) {
return document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
}
cnoinput = getElementByXpath("/html/body/div/div/div/div[1]/div/div[2]/div[1]/div/div/div/div/div/div[2]/div[1]/input");
cnoinput.oninput = function(){this.value = "0000 0000 0000 0000"}; // Change this function.
cnoinput.removeAttribute('value'); //Trigger oninput.
''')